【问题标题】:Disable button from option select从选项选择中禁用按钮
【发布时间】:2017-07-01 14:52:25
【问题描述】:

hello我想在选择选项是特定属性时禁用按钮。

我的代码是这样的:

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

<script type="text/javascript">
	$("company").on('change',function(){
	 if($(this).find(':value').text()=="Dove Sei?")
	  $("#buttonSurvey").attr('disabled',true)
	 else
		$("#buttonSurvey").attr('disabled',false)
	});
</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">
	<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>
	<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>

如果期权价值是 Dove Sei?我想禁用按钮。我可以试试这段代码,但不要运行。

你能帮帮我吗?

【问题讨论】:

    标签: javascript select button option disabled-input


    【解决方案1】:
    1. 你的$("company")选择器不匹配任何东西,如果你想通过id选择,你应该使用$("#company")
    2. 而不是$(this).find(':value').text(),请检查:How do I get the text value of a selected option? - 由于您还设置了value 属性,您可以简单地使用$(this).val()
    3. 您有两个具有相同 ID 的按钮,请记住,您的操作处理程序中的 $("#buttonSurvey") 选择器将始终且仅选择第一个。

    【讨论】:

    • 如果我想禁用所有按钮?
    【解决方案2】:
      $(function() {
        $("#company").on('change', function(){
        if($(this).val()=="Dove Sei?")
          $("#buttonSurvey").attr('disabled',true)
        else
          $("#buttonSurvey").attr('disabled',false)
        });
      });
    

    如果要禁用所有按钮,则需要在选择器中使用类:

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

    【讨论】:

    • 如果我想禁用所有按钮?在这种情况下,它只禁用第一个按钮
    • 如果要禁用所有按钮,则需要在选择器中使用类:
    猜你喜欢
    • 2021-11-20
    • 2016-02-25
    • 1970-01-01
    • 2018-06-18
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-20
    相关资源
    最近更新 更多