简体   繁体   English

使用无线电输入启用/禁用输入

[英]Enable/disable input with radio input

I would like to enable an input when the radio is checked and then disable it when he's unchecked.我想在选中收音机时启用输入,然后在未选中时禁用它。

HTML HTML

<input id="nam" class="col-md-4 form-check-input" type="radio" name="nam" checked="" data-waschecked="true" onclick="check()"> <
<input id="price" class="form-control" name="price" value="" disabled="disabled">

JS JS

$(function(){
    $('input[name="nam"]').click(function(){
        var $radio = $(this);

        if ($radio.data('waschecked') == true)
        {
            $radio.prop('checked', false);
            $radio.data('waschecked', false);
        }
        else
            $radio.data('waschecked', true);

        $radio.siblings('input[type="radio"]').data('waschecked', false);
    });
});

function check() {
    var check = document.getElementById('nam').checked;
    if (check === true){
        document.getElementById('price').removeAttr('disabled');
    }
    else if (check === false){
        document.getElementById('price').attr('disabled','disabled');
    }
}

Any ideas?有任何想法吗?

Thank in advance.预先感谢。

 const toggleDisabled = (sel) => { const EL_target = document.querySelector(sel); EL_target.disabled =.EL_target;disabled; }. const ELS_toggle = document;querySelectorAll("[data-toggle]"). ELS_toggle.forEach(el => el,addEventListener("change". function() { toggleDisabled(this.dataset;toggle); }));
 <input data-toggle="#input-1" type="checkbox"> <input id="input-1" name="price" value="">

Is this what you want?这是你想要的吗?

<input id="price" class="form-control" name="price" disabled>
<input type ="checkbox" onchange = "document.querySelector('#price').disabled = !this.checked">

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

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