IF语句不适用于jquery

你好,我需要帮助,这是我的代码,它总是去其他条件 如果值和文本匹配,我想要comapre下拉文本然后我想隐藏一些div:IF语句不适用于jquery

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>

回答:

我想comapre从下拉列表中选择文本

它看起来像你想选择的选项不是值的文本匹配,那么就应该用.text()获取文本,而不是.val()那将回退RN值,选择应该是这样的:

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");

}

});

<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>

回答:

的错误是已分配的期权价值数点&你用它进行比对,它会在if声明中返回false。但

如果你.text()比较它从每一个选项 这是所有文本if语句

$("#myid").change(function(){  

if($(this).val()=='Well,this is a dummy text') {

alert('hello');

}

else

alert("wolrd");

});

<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>

以上是 IF语句不适用于jquery 的全部内容, 来源链接: utcz.com/qa/263370.html

回到顶部