如何将文本框值与下拉列表的选项值进行比较

how to compare text box value with the option value of dropdown

本文关键字:选项 比较 下拉列表 文本      更新时间:2023-09-26

我想将文本框值与下拉列表的选项值进行比较。
如果它们是相同的警报框,则应出现。

<input type="text" name="textbox" />
<select name="selectvalue">
    <option value="1">1</option>
    <option value="2">2</option>
    <option value="3">3</option>
</select>
<script>
 $("select[name=selectvalue]").change(function(){
     if( $(this).val() == $("input[name=textbox]").val() )
         alert("Value is same!!!");
 })
</script>

您也可以在提交时或随时触发此检查。您没有提到何时需要警报。更改选择值或更改输入文本框值或单击按钮时。

// html code
<input type="text" id="textboxID" value="1" />
<select id="selectID">
    <option value="1" selected>First</option>
</select>
// the script
<script type="text/javascript">
    if ($("#textboxID").val() === $("#selectID").val())
        alert("The values are equal.");
</script>