2017-02-13 75 views
0

你好,我想禁用按鈕時,選擇選項是一個特定的屬性。禁用選項選擇按鈕

我的代碼是這樣的:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<script type="text/javascript"> 
 
\t $("company").on('change',function(){ 
 
\t if($(this).find(':value').text()=="Dove Sei?") 
 
\t $("#buttonSurvey").attr('disabled',true) 
 
\t else 
 
\t \t $("#buttonSurvey").attr('disabled',false) 
 
\t }); 
 
</script>
<select class="selectpicker" data-live-search="true" id="company" data-size="5" title="Dove Sei?" data-width="100%"> 
 
    <option data-tokens="Dove sei?" value="Dove Sei?" id="Dove Sei?" selected>Dove sei?</option> 
 
    <option data-tokens="another" id="another" value="another" data-picture="another.png">another</option> 
 
    <option data-tokens="another" id="another" value="another" data-picture="another.png">another</option> 
 
    <option data-tokens="another" id="another" value="another" data-picture="another.png">another</option> 
 
</select> 
 

 

 

 
<ul class="list-group" style="text-align: center;"> 
 
<li class="list-group-item" style="border-style: none;"><h4>Servizio</h4> 
 
<div class="btn-group"> 
 
\t <button id="buttonSurvey" type="submit" class="btn btn-default" data-toggle="modal" data-target="#voteReg" name="survey1" value="green" onclick="getVote(this.value)" style="border: none;"><img src="Q1.png}" class="Vote"></button> 
 
\t <button id="buttonSurvey" type="submit" class="btn btn-default" data-toggle="modal" data-target="#voteReg" name="survey1" value="green" onclick="getVote(this.value)" style="border: none;"><img src="Q2.png}" class="Vote"></button> 
 
</div></li>

如果選項值爲鳩SEI?我想禁用按鈕。我可以試試這個代碼,但不要運行。

你能幫我嗎?

回答

1
  1. $("company")選擇不匹配任何東西,如果你想通過編號來選擇,你應該使用$("#company")
  2. 相反的$(this).find(':value').text(),請檢查:How do I get the text value of a selected option? - 既然你還設置了value屬性,你可以簡單地使用$(this).val()
  3. 你必須使用相同的ID兩個按鈕,請記住,在$("#buttonSurvey")選擇在動作處理程序將始終並只選擇第一個。
+0

如果我想禁用所有的按鈕? –

1
$(function() { 
    $("#company").on('change', function(){ 
    if($(this).val()=="Dove Sei?") 
     $("#buttonSurvey").attr('disabled',true) 
    else 
     $("#buttonSurvey").attr('disabled',false) 
    }); 
    }); 

如果你想禁用所有的按鈕,你需要選擇使用類:

$(".btn-default").attr('disabled',true) 
+0

如果我想禁用所有按鈕?在這種情況下,它僅禁用第一個按鈕 –

+0

如果要禁用所有按鈕,則需要在選擇器中使用class: –

+0

Thankyou現在可以工作 –