简体   繁体   English

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

[英]Enable/Disable select/dropdown and Radio button on dropdown selected?

I'm trying to Enable/Disable the Radio button and select/Dropdown on dropdown selected.我正在尝试启用/禁用单选按钮并在选择的下拉列表中选择/下拉。

Jsfiddle link Jsfiddle 链接

Ex: If persons is selected only name="persons" (Anthony and Paul) and person dropdown must be available to select and Rest must be disabled例如:如果选择了 people,则仅选择name="persons" persons Anthony 和 Paul),并且必须禁用 select 和 Rest 的person dropdown

        <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/> 

What I have tried:我试过的:

Only Radio buttons without select/dropdown,只有单选按钮没有选择/下拉,

    $(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");
    })

You can loop through all radio button which have same name and enabled that radio button.Also,to enable select-box use $("select[name='" + $(this).val() + "']").prop("disabled", false);您可以遍历所有具有相同名称的单选按钮并启用该单选按钮。此外,要启用选择框,请使用$("select[name='" + $(this).val() + "']").prop("disabled", false); . .

Demo Code :演示代码

 $(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/>

 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/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM