如何使用javascript比较2个选择选项值?
到目前为止,这是我的代码:
echo "
<div class='control-group'>
<label class='control-label' for='focusedInput'>Color Combination $x</label>
<div class='controls'>
<select name='color1[]' class='form-control' id='color1' onchange='getcolor()' required>
";
$statement = $db->prepare("SELECT * FROM color order by colorName ASC");
$statement->execute();
while($rows = $statement->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='" .$rows['colorName']. "'>" . $rows['colorName'] . " - " . $rows['colorCode'] ."</option>";
}
echo "</select>";
echo " <select name='color2[]' class='form-control' onchange='getcolor()' id='color2'><option value=''>N/A</option>";
$statement = $db->prepare("SELECT * FROM color order by colorName ASC");
$statement->execute();
while($rows = $statement->fetch(PDO::FETCH_ASSOC)) {
echo "<option value='" .$rows['colorName']. "'>" . $rows['colorName'] . " - " . $rows['colorCode'] ."</option>";
}
echo "</select>";
如何比较color1
和color2
以确定它们是否相同?
以下是仅验证第一列的JavaScript:
function getcolor() {
var colorcombination1=(document.getElementById("color1").value);
var colorcombination2=(document.getElementById("color2").value);
if (colorcombination1 == 'BLACK' && colorcombination2 == 'BLACK') {
alert("Same color");
}
它只验证第一列,但我有6列需要动态验证。每当我更改颜色数量时,下拉列表的数量就会显示出来。
我不能在这里发布图片,因为它需要10点声望,我是新来的。
您能帮助我如何验证其他下拉列表吗?谢谢!
答案 0 :(得分:1)
$rows = $rows2;
。if ( colorcombination1 == colorcombination2 ) {
alert( "Same color" );
}