javascript - 未选中返回盒子上的属性(property)

标签 javascript jquery checkbox properties

如果未选中复选框,则尝试将表单控件属性返回为“启用”。 因此,对于这个 HTML 片段:

<div id="post">
    <div class="Response">
        <label><input type="radio" name="Radio402" value="Y" id="R402Y" onchange='radioChange(this, "402")'>Yes</label>
    </div>

    <div class="responseDetails">
        <div class="Observation">
            <label for="Obs402">Observation:</label>
            <textarea name="observation" id="Obs402" rows="6" disabled></textarea>
        </div>
        <div class="DueDate">
            <label for="DueDate402">Due date:<br></label>
            <input name="DueDate" class="DueDate_in" type="text" id="DueDate402"/>
        </div>
        <div class="actions">
            <label for="pa402">Actions required to correct and/or prevent this observation:</label>
            <textarea name="actions" id="pa402" rows="6"></textarea>
        </div>?
    </div>
</div>

<label for="item">Check me</label>
<input type="checkbox" id="item"/>

我正在应用这个JS

$(document).ready(function()
{
    if($("#item").click(function()
    {
        $("#post").prop("disabled", true);
    }));
    else
    {
        $("#post").prop("disabled", false);
    };
});

也尝试过

$("#item").change(function(){
    if (this.checked) {
        $("#post").prop("disabled", true);
    } else {
        $("#post").prop("disabled", false);
    }
});

如果选中,它将被禁用,但如果未选中,它将保持禁用状态。我究竟做错了什么?我还尝试了change(function)()。我感觉 JS 没有看到取消选中事件,因此不会返回该属性。提前致谢。

更新:哦伙计们,忘了提一下,有一个 textarea 元素默认保持禁用状态,并由其他地方的 radiobtn 控制。当未选中该框时,此文本字段不应启用。

最佳答案

您想使用:checked

$('#item').on('click', function(){
    $("#post").prop("disabled", $(this).is(':checked'));
});

示例:http://jsfiddle.net/VTtw5/

关于javascript - 未选中返回盒子上的属性(property),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22268346/

相关文章:

javascript - 创建粘性页脚

javascript - 自动调整文本元素大小不适用于 keyup

javascript - 为什么从模型获取值时我的引导日期选择器日历不更新?

jquery - Rails 3 ajax 调用 - ActionView::MissingTemplate

javascript - jQuery - 如果用户选择了特定复选框,则取消选中其他复选框

html - 使用 CSS 查找复选框的标签

javascript - 选择指定金额后禁用复选框

javascript - 在nightmarejs中有一个foreach方法吗?

javascript - 数组递增 Javascript/node js/mongodb

checkbox - Vaadin - 如何将复选框组件添加到树中?