你好,我需要帮助这个代码,它总是进入其他条件 如果值和文本匹配,我想从下拉列表中抄送文本,然后我想隐藏一些div:
jQuery("#myid").on('change', function () {
if(jQuery('#myid').val() == 'Well, this is a dummy text') {
alert(" hello");
} else {
alert("World");
}
});
<select name="testtype" id="myid" class="validate[required]" style="font-size:12px; font-family:arial">
<option selected="selected" value="">-- Make your selection --</option>
<option id="first" value="Health, Safety and Environment Test for Operatives ">Health, Safety and Environment Test for Operatives </option>
<option value="Supervisory-English only">Supervisory-English only</option>
<option value="Health, Safety and Environment Test for Managers and Professionals -English only">Health, Safety and Environment Test for Managers and Professionals -English only</option>
<option value="Demolition-English only">Demolition-English only</option>
<option value="Highway Works-English only">Highway Works-English only</option>
<option value="Specialist Work at Hight-English only">Specialist Work at Hight-English only</option>
<option value="Lifts and Escalators-English only">Lifts and Escalators-English only</option>
<option value="Tunnelling-English only">Tunnelling-English only</option>
<option value="Heating, Ventilation Air Conditioning and Refrigeration (HAVCR)-English only">Heating, Ventilation Air Conditioning and Refrigeration (HAVCR)-English only</option>
<option value="Plumbing (JIB)-English only">Plumbing (JIB)-English only</option>
</select>
答案 0 :(得分:1)
我想从下拉列表中抄送文本
看起来您想要匹配所选选项的文本而不是值,那么您应该使用.text()
而不是.val()
来获取文本,这将返回值,选择器应该像:
jQuery('#myid option:selected').text()
代码:
jQuery("#myid").on('change', function() {
//if (jQuery('#myid option:selected').text() == ...){
//Or
if (jQuery(this).val() == 'Health, Safety and Environment Test for Operatives ') {
alert(" hello");
} else {
alert("World");
}
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="testtype" id="myid" class="validate[required]" style="font-size:12px; font-family:arial">
<option selected="selected" value="">-- Make your selection --</option>
<option id="first" value="Health, Safety and Environment Test for Operatives ">Health, Safety and Environment Test for Operatives </option>
<option value="Supervisory-English only">Supervisory-English only</option>
<option value="Health, Safety and Environment Test for Managers and Professionals -English only">Health, Safety and Environment Test for Managers and Professionals -English only</option>
<option value="Demolition-English only">Demolition-English only</option>
<option value="Highway Works-English only">Highway Works-English only</option>
<option value="Specialist Work at Hight-English only">Specialist Work at Hight-English only</option>
<option value="Lifts and Escalators-English only">Lifts and Escalators-English only</option>
<option value="Tunnelling-English only">Tunnelling-English only</option>
<option value="Heating, Ventilation Air Conditioning and Refrigeration (HAVCR)-English only">Heating, Ventilation Air Conditioning and Refrigeration (HAVCR)-English only</option>
<option value="Plumbing (JIB)-English only">Plumbing (JIB)-English only</option>
</select>
&#13;
答案 1 :(得分:0)
错误是您已将选项的值分配给数字和&amp;您正在与它进行比较,因此它在if
语句中返回false。但
如果您与
中失败的原因.text()
进行比较,则会获得每个选项的所有文字 这就是为什么它在if语句
$("#myid").change(function(){
if( $(this).val()=='Well,this is a dummy text' ) {
alert('hello');
}
else
alert("wolrd");
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="myid">
<option value="1">wolrd</option>
<option value="2">hello</option>
<option value="Well,this is a dummy text">Well,this is a dummy text</option>
</select>
&#13;