gpt4 book ai didi

JavaScript 无法选择复选框

转载 作者:行者123 更新时间:2023-11-28 15:35:39 27 4
gpt4 key购买 nike

我有一个复选框。我正在尝试使用 another SO question 中的示例以确定它是否被选中。

但是,我似乎只能使用 document.getElementById 查询元素的状态。我想让它按照 Stack Overflow 示例运行。代码如下...

<html>
<br>
<label>Select All BOM's</label>
<input type="checkbox" id="allboms" name="degbutton"/>
<br>


function doSomething()
{
//does not work
if ($("#allboms").checked){
//Do stuff

alert('was checked');
}

//does not work
if ($("#allboms").attr("checked")) {

//Do stuff
console.log("boooooooooooo");
alert('i1 was checked');
}

//works
if(document.getElementById("allboms").checked){
//Do stuff

alert('is checked ');
}else{
alert('is unchecked .....');
}
</html>

最佳答案

这些是我能想到的使用纯 Javascript 和 jQuery 的所有形式:

$(document).ready(function rd(){
var $allboms = $('#allboms');

$allboms.on('change', function ch(){
if (this.checked) {
console.log('this.checked is true.');
}

if ($(this)[0].checked) {
console.log('$(this)[0].checked is true.');
}

if ($(this).prop('checked')) {
console.log('$(this).prop("checked") is true.');
}

if ($(this).is(':checked')) {
console.log('$(this).is(":checked") is true.');
}
});
});

http://jsfiddle.net/dyeu7w77/

关于JavaScript 无法选择复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25700315/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com