我无法检测到从下面的脚本中单击了何时以及哪个复选框:
HTML 片段:
<label for="checkbox[1]"><input type="checkbox" name="checkbox[1]" id="checkbox[1]" class="detectThisChange" value="10.00" checked=""> Amount $10.00</label>
<label for="checkbox[2]"><input type="checkbox" name="checkbox[2]" id="checkbox[2]" class="detectThisChange" value="20.00" checked=""> Amount $20.00</label>
<label for="checkbox[3]"><input type="checkbox" name="checkbox[3]" id="checkbox[3]" class="detectThisChange" value="30.00" checked=""> Amount $30.00</label>
jQuery 片段:
$(document).ready(function() {
$(window).load(function() {
// ... //
$('.detectThisChange').change(function(event){
var targetID = triggerEvent.target.id; // get the id that triggered the event
var posStart = targetID.indexOf('[') + 1;
var posEnd = targetID.indexOf(']');
var i = targetID.substring(posStart, posEnd); // get the index of the id that triggered the event
if ( $('#checkbox\\['+ i +'\\]').prop('checked') != true ) {
alert('checkbox ' + i + ' was checked');
}
else {
alert('checkbox ' + i + ' was unchecked');
}
});
// ... //
}); // end .load()
}); // end .ready()
附:
我遇到的问题是我的警报都不起作用。这告诉我change()函数没有触发。
如果要动态添加此 HTML,则应使用 .on() 方法,例如:
$(document).on('change', '.detectThisChange', function() {
// your code
});
尝试一下,让我知道是否有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)