简体   繁体   English

复选框和单击事件处理

[英]Checkbox and click event handling

I'm trying to set a textbox to 'readonly', add a class, and put a text into the textbox at that moment when I check the checkbox. 我正在尝试将文本框设置为“只读”,添加一个类,然后在选中该复选框时将文本放入文本框。 Moreover, I'm also trying to remove 'readonly' attribute from the textbox, add a class, and delete text in the textbox. 此外,我还尝试从文本框中删除“ readonly”属性,添加一个类,然后删除文本框中的文本。

I have 我有

$('#CheckBoxSectionCode').click(function () {
    if ($(this).is(':checked')) {
        $('#TextBoxSectionCode').attr('readonly', 'readonly');
        $('#TextBoxSectionCode').addClass('disabled');
        $('#TextBoxSectionCode').text(document.getElementById('TextBoxSectionName').val);
        }
    else {
        $('#TextBoxSectionCode').attr('readonly', false);
        $('#TextBoxSectionCode').addClass('abled');
        $('#TextBoxSectionCode').text('');
    }
});

This code doesn't work for me. 此代码对我不起作用。

Thanks, 谢谢,

Phillip 菲利普


Thanks everyone for answers. 谢谢大家的回答。

According to your comments and answers, I've changed my code but it's still not working. 根据您的评论和答案,我已经更改了代码,但仍然无法正常工作。

$('#CheckBoxSectionCode').click(function () {
    if ($(this).is(':checked')) {
        $('#TextBoxSectionCode').prop('readonly', true);
        $('#TextBoxSectionCode').addClass('disabled');
        $('#TextBoxSectionCode').text('disabled');

    }
    else {
        $('#TextBoxSectionCode').prop('readonly', false);
        $('#TextBoxSectionCode').removeClass('disabled').addClass('enabled');
        $('#TextBoxSectionCode').text('');
    }
});

I'm using chrome browser to run this code, and using developer tools in chrome and put a break point at the code above to see what's happening in the jquery. 我正在使用chrome浏览器运行此代码,并在chrome中使用开发人员工具,并在上面的代码处添加了一个断点,以查看jquery中发生的事情。 However, when I click the check box to check/uncheck, nothing happens there. 但是,当我单击复选框以选中/取消选中时,没有任何反应。

document.getElementById('TextBoxSectionName').val this is wrong. document.getElementById('TextBoxSectionName').val这是错误的。 You really should cache your jQuery object so it's not navigating the DOM over and over. 您确实应该缓存您的jQuery对象,以便它不会一遍又一遍地浏览DOM。 Then you mix in native JS and .val is not a DOM property or method, nor is it a jQuery property, it should be .value for a DOM object or .val() for a jQuery object. 然后混入本机JS,.val不是DOM属性或方法,也不是jQuery属性,对于DOM对象,它应该是.value ;对于jQuery对象,它应该是.val()

Obligatory explanation by @Archy Wilhes: @Archy Wilhes的强制性解释:

"Just to clarify; when @SterlingArcher says caching the jQuery object, she/he means doing something like var obj = $('#TextBoxSectionCode') then calling the functions using the variable like this: obj.attr(...); obj.addClass(...). Every time you do a $(something) you are calling a function in jQuery that looks for the DOM." “澄清一下,当@SterlingArcher说缓存jQuery对象时,她/他的意思是做类似var obj = $('#TextBoxSectionCode')的操作,然后使用像这样的变量来调用函数:obj.attr(...); obj.addClass(...)。每次执行$(something)时,您都在jQuery中调用一个寻找DOM的函数。”

since everytime you are adding the class the element is going to end up having both the two classes. 因为每次添加类时,元素最终都会同时具有两个类。 Consider removing the other class before adding one. 在添加一个类之前,请考虑删除另一个类。 For example, 例如,

$(selector).removeClass('disabled').addClass('enabled')

Try with change event instead of click: 尝试更改事件,而不是单击:

    $('#CheckBoxSectionCode').change(function () {
        if ($(this).is(':checked')) {
        $('#TextBoxSectionCode').attr('readonly', 'readonly');
        $('#TextBoxSectionCode').addClass('disabled');
        $('#TextBoxSectionCode').text(document.getElementById('TextBoxSectionName').val);
    }
    else {
        $('#TextBoxSectionCode').attr('readonly', false);
        $('#TextBoxSectionCode').addClass('abled');
        $('#TextBoxSectionCode').text('');
    }

});

You could do the following way. 您可以按照以下方式进行。

//Cache reference to DOM as DOM scan is expensive!
var textBox = $('#TextBoxSectionCode');
$('#CheckBoxSectionCode').click(function () {
    //Use prop as opposed to attr
    textBox.prop("readOnly", false).removeClass('disabled').addClass('abled').text("");
    if ($(this).is(':checked')) {
        textBox.prop("readOnly", true).removeClass('abled').addClass('disabled').text($("#TextBoxSectionName").val());
    }
});

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

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