Javascript HTML下拉验证
我希望能够将验证添加到我的下拉列表中。如果用户在第一个下拉列表(选择1)中选择加的夫,然后在第二个下拉列表(选择2)中再次选择加的夫,则If应向用户发出警告消息,说明“目的地不能与出发地相同” 我知道这对用户不友好,但需要这样做=S 我在第二个下拉列表中遇到了一些问题,因为它似乎不像double和if语句那样有效 请使用jsfiddle.net帮助我,谢谢Javascript HTML下拉验证,javascript,html,Javascript,Html,我希望能够将验证添加到我的下拉列表中。如果用户在第一个下拉列表(选择1)中选择加的夫,然后在第二个下拉列表(选择2)中再次选择加的夫,则If应向用户发出警告消息,说明“目的地不能与出发地相同” 我知道这对用户不友好,但需要这样做=S 我在第二个下拉列表中遇到了一些问题,因为它似乎不像double和if语句那样有效 请使用jsfiddle.net帮助我,谢谢 Departure: <br> <select id="select1"> <option val
Departure: <br>
<select id="select1">
<option value="" disabled selected>Please Select</option>
<option>Birmingham</option>
<option>Bristol</option>
<option>Cardiff</option>
<option>Liverpool</option>
<option>London</option>
<option>Manchester</option>
</select>
<br><br>
Destination: <br>
<select id="select2">
<option value="" disabled selected>Please Select</option>
<option>Birmingham</option>
<option>Bristol</option>
<option>Cardiff</option>
<option>Liverpool</option>
<option>London</option>
<option>Manchester</option>
</select>
<br><br>
<input type="Button" value="Order Tickets" onClick="myFunction()">
<script>
function myFunction()
{
if (document.getElementById("select1" && "select2").value == "Cardiff")
alert("Destination can't be same as Departure.");
else
alert("That's Fine");
}
</script>
出发:
请选择
伯明翰
布里斯托尔
加的夫
利物浦
伦敦
曼彻斯特
目的地:
请选择
伯明翰
布里斯托尔
加的夫
利物浦
伦敦
曼彻斯特
函数myFunction()
{
if(document.getElementById(“select1”和“select2”)。value==“Cardiff”)
警报(“目的地不能与出发地相同。”);
其他的
警惕(“那很好”);
}
您需要获取两个选择的值
var one=document.getElementById("select1");
var two=document.getElementById("select2");
if (one.options[one.selectedIndex].value == two.options[two.selectedIndex].value)
alert("Destination can't be same as Departure");
最好使用唯一的名称作为选项值
Departure: <br>
<select id="select1">
<option value="" disabled selected>Please Select</option>
<option value="1">Birmingham</option>
<option value="2">Bristol</option>
<option value="3">Cardiff</option>
<option value="4">Liverpool</option>
<option value="5">London</option>
<option value="6">Manchester</option>
</select><br><br>
Destination: <br>
<select id="select2">
<option value="" disabled selected>Please Select</option>
<option value="1">Birmingham</option>
<option value="2">Bristol</option>
<option value="3">Cardiff</option>
<option value="4">Liverpool</option>
<option value="5">London</option>
<option value="6">Manchester</option>
</select><br><br>
<input type="Button" value="Order Tickets" onClick="myFunction()">
<script>
function myFunction() {
if (document.getElementById("select1").value == document.getElementById("select2").value)
alert("Destination can't be same as Departure.");
else
alert("That's Fine");
}
</script>
出发:
请选择
伯明翰
布里斯托尔
加的夫
利物浦
伦敦
曼彻斯特
目的地:
请选择
伯明翰
布里斯托尔
加的夫
利物浦
伦敦
曼彻斯特
函数myFunction(){
if(document.getElementById(“select1”).value==document.getElementById(“select2”).value)
警报(“目的地不能与出发地相同。”);
其他的
警惕(“那很好”);
}
或者更好:
function myFunction() {
if (document.getElementById("select1").value === document.getElementById("select2").value)
alert("Destination can't be same as Departure.");
else
alert("That's Fine");
}
替换
if (document.getElementById("select1" && "select2").value == "Cardiff")
与
此外,如果你把出发点从目的地名单上去掉,你会过得更好。但是你问题中的代码听起来对你目前的技能来说有点太多了。我已经更新了你的JavaScript。这是密码
function myFunction() {
var dest1 = document.getElementById("select1").value;
var dest2 = document.getElementById("select2").value;
if (dest1 === dest2)
alert("Destination can't be same as Departure.");
else
alert("That's Fine");
}
这到底是什么
document.getElementById(“select1”和“select2”)
这等于document.getElementById(“select2”)
@MarcosPérezGude-An语句?您似乎知道如何选择单个元素。比较这两个元素的值。忘记卡迪夫吧,这与卡迪夫无关。@MarcosPérezGude OP显然有问题,不太理解如果和/或getElementById
@Imraanstack2将其放入javascript控制台:“select1”和“select2”
。结果如何?和getElementById()
这是一个方法或函数,可以随意调用,但不能在其中使用And语句
if (document.getElementById("select1").value == document.getElementById("select1").value ) {
...
}
function myFunction() {
var dest1 = document.getElementById("select1").value;
var dest2 = document.getElementById("select2").value;
if (dest1 === dest2)
alert("Destination can't be same as Departure.");
else
alert("That's Fine");
}