私のフォームでは、javascript を使用してユーザー入力を検証しましたが、2 つの異なるドロップダウン メニューの値を比較して、それらが同じでないことを確認したいと考えています。「ピックアップ場所」と「目的地」の 2 つのドロップダウン メニューには、それぞれに利用可能なまったく同じ町名が含まれているため、ユーザーは両方に同じ町を簡単に選択できますが、これは望ましくありません。可能であればこれを達成する方法はありますか?ありがとう
コード:
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>