javascript - 如何在单击单选按钮之一时启用文本框

标签 javascript html

我使用 bootstrap 3 在 html 页面中有 4 个单选按钮和一个文本框,我想默认禁用文本框并启用当单选按钮 EnterRange 启用时它。有什么建议吗?

<script type="text/javascript">
    $(".radioBtn").click(function() {
    $("#dist").attr("disabled", true);
        if ($("input[name=optradio]:checked").val() == "EnterRange") {
            $("#txtbox").attr("disabled", false);
        }
    });
</script>

html代码:

<div class="customradio">
    <input class="radioBtn" type="radio" id="5" name="optradio" value="5" checked="checked">
    <label for="5">5 Km</label>
</div>
<div class="customradio">
    <input class="radioBtn" type="radio" id="10" name="optradio" value="10">
    <label for="10">10 Km</label>
</div>
<div class="customradio">
    <input class="radioBtn" type="radio" id="15" name="optradio" value="15">
    <label for="15">15 Km</label>
</div>
<div class="customradio">
    <input class="radioBtn" type="radio" id="EnterRange" name="optradio" value="EnterRange">
    <label for="EnterRange">Enter Range</label>
</div>
<div>
   <input type="text" id="dist" name="txtbox" disabled="disabled"> <label>Km</label>
</div>

最佳答案

这里只有一个错误

if ($("input[name=optradio]:checked").val() == "EnterRange") {
 $("#dist").attr("disabled", false);  //selector is wrong.
}

您刚刚将 name 作为 selector 实际上您的代码是完美的,您只需将此 selector 更改为 $("#txtbox")$("#dist")

$(".radioBtn").change(function() {
    $("#dist").attr("disabled", true);
    alert($("input[name=optradio]:checked").val())
        if ($("input[name=optradio]:checked").val() == "EnterRange") {
            $("#dist").attr("disabled", false);
        }
    });

关于javascript - 如何在单击单选按钮之一时启用文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40994064/

相关文章:

html - 从 Yahoo! 上抓取头条新闻使用 Python3 进行财务

javascript - Javascript中的多个对象属性分配

javascript - 有什么办法可以在它的 64 位浮点 IEEE754 表示中看到一个数字

javascript - 暂时禁用 CSS 过渡效果的最干净的方法是什么?

JavaScript默认函数值

javascript - 初始化后禁用 Kendo Calendar 中的日期

javascript - Liquid Grid——人们如何调用它

php - 表单自动填充字段填充不正确

html - 许多行内 block 元素的边距

html - Bootstrap 搜索输入动态居中