2017-12-18 98 views
0

你好,我需要幫助,這是我的代碼,它總是去其他條件 如果值和文本匹配,我想要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> 
+1

你怎麼能指望任何人幫助與失敗的原因在?只有你可以知道'jQuery('#myid')。val()'是什麼,因此條件失敗。 – str

+0

向我們展示Html代碼! –

+0

什麼元素屬於#myid –

回答

2

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

+0

嗯,這是一個虛擬文本,它是文本和值也不會提示你好 – eliana

+0

好的,請向我們展示您的下拉HTML代碼的版本。 –

+0

查看問題,我想在 eliana

1

的錯誤是已分配的期權價值數點&你用它進行比對,它會在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>