禁用选项选择按钮

问题描述:

你好,我想禁用按钮时,选择选项是一个特定的属性。禁用选项选择按钮

我的代码是这样的:

<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. $("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

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

$(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现在可以工作 –