我試圖設置文本框爲'只讀',添加一個類,並在當時檢查複選框時將文本放入文本框中。此外,我還試圖從文本框中刪除「只讀」屬性,添加一個類並刪除文本框中的文本。複選框並單擊事件處理
我
$('#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沒問題。我認爲你的評論非常可靠。你應該把它變成一個答案:) –