为了账号安全,请及时绑定邮箱和手机立即绑定

启用/禁用选择/下拉菜单和下拉单选按钮?

启用/禁用选择/下拉菜单和下拉单选按钮?

收到一只叮咚 2022-10-21 15:10:03
我正在尝试启用/禁用单选按钮并在选择的下拉列表中选择/下拉。Jsfiddle 链接例如:如果persons仅被选中name="persons"(Anthony 和 Paul)并且person dropdown必须可供选择并且必须禁用 Rest     <select class="browser-default" id="type" name="type">        <option value="persons">persons</option>        <option value="animals">animals</option>        <option value="fruits">fruits</option>        </select>        <br/>        <label><input type="radio" name="fruits" value="apple" id="apple" title="">Apple</label>        <br/>        <label><input type="radio" name="fruits" value="banana" id="banana" title="">Banana</label>        <br/>        <label><input type="radio" name="animals" value="dog" id="dog" title="">Dog</label>        <br/>        <label><input type="radio" name="animals" value="cat" id="cat" title="">Cat</label>        <br/>        <label><input type="radio" name="persons" value="anthony" id="anthony" title="">Anthony</label>        <br/>        <label><input type="radio" name="persons" value="paul" id="paul" title="">Paul</label>        <br/>        <select class="browser-default" id="persons1" name="persons">        <option value="1">Person Dropdown</option>        </select>        <br/>         <select class="browser-default" id="animals1" name="animals">        <option value="1">Animals Dropdown</option>        </select>        <br/>         <select class="browser-default" id="fruits1" name="fruits">        <option value="1">Fruits Dropdown</option>        </select>        <br/> 我试过的:只有单选按钮没有选择/下拉,    $(document).ready(function() {    $("select").change(function() {        $("input").prop("checked", false);        $("input").prop('disabled', false);        $("input[type='radio']").prop("disabled", true);        $("input[name='" + $(this).val() + "']").prop("disabled", false);    }).trigger("change");    })
查看完整描述

2 回答

?
www说

TA贡献1775条经验 获得超8个赞

 function disabled_all(){

  $('input[name="fruits"],input[name="animals"],input[name="fruits"], select[name="persons"],select[name="animals"],select[name="fruits"]').prop('disabled', true);

  }

 

 disabled_all();

 

 $('.browser-default').on('change', function () {

    var type = $(this).val();

    disabled_all();

    $('input[name="' + type + '"], select[name="' + type + '"]').prop('disabled', false);

}).trigger('chnage');

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<select class="browser-default" id="type" name="type">

        <option value="persons">persons</option>

        <option value="animals">animals</option>

        <option value="fruits">fruits</option>

        </select>


        <br/>


        <label><input type="radio" name="fruits" value="apple" id="apple" title="">Apple</label>

        <br/>

        <label><input type="radio" name="fruits" value="banana" id="banana" title="">Banana</label>

        <br/>


        <label><input type="radio" name="animals" value="dog" id="dog" title="">Dog</label>

        <br/>

        <label><input type="radio" name="animals" value="cat" id="cat" title="">Cat</label>

        <br/>



        <label><input type="radio" name="persons" value="anthony" id="anthony" title="">Anthony</label>

        <br/>

        <label><input type="radio" name="persons" value="paul" id="paul" title="">Paul</label>

        <br/>


        <select class="browser-default" id="persons1" name="persons">

        <option value="1">Person Dropdown</option>

        </select>

        <br/> 


        <select class="browser-default" id="animals1" name="animals">

        <option value="1">Animals Dropdown</option>

        </select>

        <br/> 


        <select class="browser-default" id="fruits1" name="fruits">

        <option value="1">Fruits Dropdown</option>

        </select>

        <br/>


查看完整回答
反对 回复 2022-10-21
?
繁星点点滴滴

TA贡献1803条经验 获得超3个赞

您可以遍历所有具有相同名称并启用该单选按钮的单选按钮。此外,启用选择框使用 $("select[name='" + $(this).val() + "']").prop("disabled", false); 。


演示代码:


$(document).ready(function() {


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

  //disable other select

   $('select:not(#type)').prop("disabled", true);

    $("input").prop("checked", false);

    $("input").prop('disabled', false);

    $("input[type='radio']").prop("disabled", true);

    //looping through all inputs

  $("input[name='" + $(this).val() + "']").each(function(){

   $(this).prop("disabled", false); //remove disable

  })

  //remove disabled from slect

 $("select[name='" + $(this).val() + "']").prop("disabled", false);

 


  }).trigger("change");

})

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>

<select class="browser-default" id="type" name="type">

  <option value="persons">persons</option>

  <option value="animals">animals</option>

  <option value="fruits">fruits</option>

</select>


<br/>


<label><input type="radio" name="fruits" value="apple" id="apple" title="">Apple</label>

<br/>

<label><input type="radio" name="fruits" value="banana" id="banana" title="">Banana</label>

<br/>


<label><input type="radio" name="animals" value="dog" id="dog" title="">Dog</label>

<br/>

<label><input type="radio" name="animals" value="cat" id="cat" title="">Cat</label>

<br/>



<label><input type="radio" name="persons" value="anthony" id="anthony" title="">Anthony</label>

<br/>

<label><input type="radio" name="persons" value="paul" id="paul" title="">Paul</label>

<br/>


<select class="browser-default" id="persons1" name="persons">

  <option value="1">Person Dropdown</option>

</select>

<br/>


<select class="browser-default" id="animals1" name="animals">

  <option value="1">Animals Dropdown</option>

</select>

<br/>


<select class="browser-default" id="fruits1" name="fruits">

  <option value="1">Fruits Dropdown</option>

</select>

<br/>


查看完整回答
反对 回复 2022-10-21
  • 2 回答
  • 0 关注
  • 89 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信