简体   繁体   English

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

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

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

jsfiddle link jsfiddle链接

Ex: If persons is selected only name="persons" must be available to select(Anthony and Paul), Rest must be disabled例如:如果选择了persons ,则只有name="persons"必须可供选择(Anthony 和 Paul),必须禁用 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" >Apple</label>
        <br/>
        <label><input type="radio" name="fruits" value="banana" id="banana" >Banana</label>
        <br/>

        <label><input type="radio" name="animals" value="dog" id="dog" >Dog</label>
        <br/>
        <label><input type="radio" name="animals" value="cat" id="cat" >Cat</label>
        <br/>


        <label><input type="radio" name="persons" value="anthony" id="anthony" >Anthony</label>
        <br/>
        <label><input type="radio" name="persons" value="paul" id="paul" >Paul</label>
        <br/>

There you go:你有 go:

 $(document).ready(function(){ $('select').on('change',function(){ $('input').attr('disabled',true) $('input[name="'+$(this).val()+'"]').removeAttr('disabled') }) })
 <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" >Apple</label> <label><input type="radio" name="fruits" value="banana" id="banana" >Banana</label> <label><input type="radio" name="animals" value="dog" id="dog" >Dog</label> <label><input type="radio" name="animals" value="cat" id="cat" >Cat</label> <label><input type="radio" name="persons" value="anthony" id="anthony" >Anthony</label> <label><input type="radio" name="persons" value="paul" id="paul" >Paul</label>

But you should change your DOM structure.但是你应该改变你的 DOM 结构。

Well to make this work, you just need to check the current value of select then make a condition on which input has to be disabled .要想完成这项工作,您只需要检查select的当前值,然后确定必须禁用输入的条件

Pure JS approach纯 JS 方法

So all you have to do is to make an addEventListener for your select element change, then set the condition for disabling the associated input s with the same name of the selected option.因此,您所要做的就是为您的select元素更改创建一个addEventListener ,然后设置禁用与所选选项name的关联input的条件。 Then with the page onload event, we will just trigger the change event of select with the event constructor to make sure our event listener runs once in page load.然后使用页面加载事件,我们将使用事件构造函数触发onload的更改事件,以确保我们的事件监听器在页面加载时运行一次。

 const select = document.getElementById("type"); const inputs = document.querySelectorAll("input[type='radio']"); window.onload = function() { select.dispatchEvent(new Event('change')); } select.addEventListener("change", function() { inputs.forEach(input => { input.checked = false; if (input.getAttribute("name").== this.value) { input,setAttribute("disabled"; true). } else { input;removeAttribute("disabled"); } }) })
 <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 />

jQuery approach jQuery方法

This is almost the same as pure approach and it just implemented by jQuery.这与纯方法几乎相同,只是由 jQuery 实现。 Here we will listen to the change event of our select element, then we will disable all the input and enable all the matched input s value with select value.在这里,我们将监听select元素的更改事件,然后我们将禁用所有输入并启用所有匹配的input s 值与select值。 In the end, we will call the trigger event to make sure our selection will change once in the page load.最后,我们将调用trigger事件来确保我们的选择会在页面加载时更改一次。

 $(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"); })
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <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" >Apple</label> <label><input type="radio" name="fruits" value="banana" id="banana" >Banana</label> <label><input type="radio" name="animals" value="dog" id="dog" >Dog</label> <label><input type="radio" name="animals" value="cat" id="cat" >Cat</label> <label><input type="radio" name="persons" value="anthony" id="anthony">Anthony</label> <label><input type="radio" name="persons" value="paul" id="paul">Paul</label>

 const type = document.querySelector("#type"); const radios = document.querySelectorAll("#radio-group input[type=radio]"); function toggleDisabled() { radios.forEach(r => r.disabled = r.name.== type;value). } type,addEventListener("change"; toggleDisabled); toggleDisabled();
 <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/> <div id="radio-group"> <label><input type="radio" name="fruits" value="apple" id="apple" >Apple</label> <br/> <label><input type="radio" name="fruits" value="banana" id="banana" >Banana</label> <br/> <label><input type="radio" name="animals" value="dog" id="dog" >Dog</label> <br/> <label><input type="radio" name="animals" value="cat" id="cat" >Cat</label> <br/> <label><input type="radio" name="persons" value="anthony" id="anthony" >Anthony</label> <br/> <label><input type="radio" name="persons" value="paul" id="paul" >Paul</label> </div>

Working code below for disabled the radio button on change of values from select dropdow based on type of genre.下面的工作代码用于disabled基于流派类型从select dropdow更改值的单选按钮。

 $(document).ready(function(){ $('select').on('change',function(){ $('input').prop('checked', false); $('input').prop('disabled', true); $('input[name="'+$(this).val()+'"]').prop('disabled', false); }) })
 <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" disabled="true">Apple</label> <label><input type="radio" name="fruits" value="banana" id="banana" disabled="true">Banana</label> <label><input type="radio" name="animals" value="dog" id="dog" disabled="true">Dog</label> <label><input type="radio" name="animals" value="cat" id="cat" disabled="true">Cat</label> <label><input type="radio" name="persons" value="anthony" id="anthony">Anthony</label> <label><input type="radio" name="persons" value="paul" id="paul">Paul</label>

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

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