我试图设置文本框为'只读',添加一个类,并在当时检查复选框时将文本放入文本框中。此外,我还试图从文本框中删除“只读”属性,添加一个类并删除文本框中的文本。复选框并单击事件处理
我
$('#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('');
}
});
此代码不会为我工作。
感谢,
菲利普
谢谢大家的答案。
根据你的意见和答案,我已经改变了我的代码,但它仍然无法正常工作。
$('#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('');
}
});
我使用的是Chrome浏览器来运行该代码,并使用镀铬开发工具,把一个破发点,在上面的代码,看看发生了什么事在jQuery的。但是,当我单击复选框来检查/取消选中时,那里没有任何反应。
只是为了澄清;当@SterlingArcher说*缓存* jQuery对象时,她/他的意思是做类似'var obj = $('#TextBoxSectionCode')'然后使用如下变量调用函数:'obj.attr(...); obj.addClass(...)'。每次你做'$(something)'时,你都会在jQuery中调用一个寻找DOM的函数。 –
@ArchyWilhes是的,非常好的解释,谢谢 –
@SterlingArcher没问题。我认为你的评论非常可靠。你应该把它变成一个答案:) –