我想問爲什麼$(this)不能在我的on函數中工作?
我的目標是隱藏輸入,如果複選框被點擊。
$('#checkboxContactName').on('change',() => {
if ($(this).prop('checked')) {
console.log('is checked!');
$(this).parent().next().css('display', 'none');
} else {
$(this).parent().next().css('display', 'block');
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<div class="form-group">
<label class="control-label" for="#" title="Contact Name">Contact</label>
<label class="checkbox-inline" style="float:right;"><input id="checkboxContactName" type="checkbox" /> Hide
</label>
</div>
變化'(=>'來'函數()' 。嘗試這個。 –
您可以在箭頭函數中使用'event.target'來引用調度事件的元素 – guest271314
將函數()=>更改爲function()並關閉標記,就像我在爲您製作的代碼段中爲您做的那樣。如果你在HTML中有一個parent()。next(),它也會很有用 – mplungjan