在我的表单上,我使用 javascript 来验证用户输入,但是我想比较两个不同下拉菜单的值以检查它们是否相同。两个下拉菜单“接送地点”和“目的地”包含完全相同的城镇名称,因此用户可以轻松地为两者选择相同的城镇,这是我不想要的。如果可能的话,任何想法如何实现这一目标?谢谢
代码:
function validateFormOnSubmit(theForm) {
var reason = "";
reason += validatepickuplocation(theForm.pickuplocation);
if (reason != "") {
alert("Some fields need correction:\n" + reason);
return false;
}
return true;
}
function validatePickuplocation(fld) {
var pickuplocation = document.getElementById("pickuplocation");
var destination = document.getElementById("destination");
if (pickuplocation.options[pickuplocation.selectedIndex].value == destination.options[destination.selectedIndex].value) {
fld.style.background = 'Yellow';
error = "Make sure 'Pick up location' and 'Destination' are not the same locations.\n"
} else {
fld.style.background = 'White';
}
return error;
}
html:
<tr>
<td><label for="pickuplocation">Pick up location:</label></td>
<td><select name="pickuplocation" size="1">
<OPTIONS>
</select></td></tr>
<tr>
<td><label for="destination">Pick up location:</label></td>
<td><select name="destination" size="1">
<OPTIONS>
</select></td></tr>