gpt4 book ai didi

javascript - 复选框一开始就不起作用

转载 作者:行者123 更新时间:2023-12-01 03:57:31 24 4
gpt4 key购买 nike

我有两个复选框,其中一个从一开始就被选中,但没有做应该做的事情,所以我必须取消选中并再次检查它才能使其工作,我怎样才能使它从一开始就工作?

$("input:checkbox").on('click', function() {
var $box = $(this);
if ($box.is(":checked")) {
var group = "input:checkbox[name='" + $box.attr("name") + "']";
$(group).prop("checked", false);
$box.prop("checked", true);
} else {
$box.prop("checked", false);
}
});

function text(obj) {
if($(obj).is(":checked")){
var x=($(obj).attr('id'));
if (x=='a'){
document.getElementById("text").innerHTML = 'something here a';
}else {
document.getElementById("text").innerHTML = 'something here b';
}

}

}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Load D3 -->
</head>
<body>
<div>
<label>
<input id="a" type="checkbox" class="radio" value='des' name="fooby[1][]" checked="" onchange='text(this)'/>option a</label>
<label>
<input id="b" type="checkbox" class="radio" value='icv' name="fooby[1][]" onchange='text(this)'/>option b</label>
</div>
<p id="text"></p>

</body>

</html>

最佳答案

我玩了一些把戏

只关注这一点

$(document).ready(function(){
var checkboxes = $('input[type=checkbox]');
checkboxes.each(function(index, value){
if($(value).is(':checked')){
$(value).click( ); // by default checked. if click now check box will be unchecked
$(value).click( );// above line uncheck the checkbox now we have to check it again, because this is what all we want
}
})
});

$(document).ready(function(){
var checkboxes = $('input[type=checkbox]');
checkboxes.each(function(index, value){
if($(value).is(':checked')){
$(value).click( ); // by default checked. if click now check box will be unchecked
$(value).click( );// above line uncheck the checkbox now we have to check it again, because this is what all we want
}
})
});

$("input:checkbox").on('click', function() {
var $box = $(this);
if ($box.is(":checked")) {
var group = "input:checkbox[name='" + $box.attr("name") + "']";
$(group).prop("checked", false);
$box.prop("checked", true);
} else {
$box.prop("checked", false);
}
});

function text(obj) {
if($(obj).is(":checked")){
var x=($(obj).attr('id'));
if (x=='a'){
document.getElementById("text").innerHTML = 'something here a';
}else {
document.getElementById("text").innerHTML = 'something here b';
}

}

}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- Load D3 -->
</head>
<body>
<div>
<label>
<input id="a" type="checkbox" class="radio" value='des' name="fooby[1][]" checked="" onchange='text(this)'/>option a</label>
<label>
<input id="b" type="checkbox" class="radio" value='icv' name="fooby[1][]" onchange='text(this)'/>option b</label>
</div>
<p id="text"></p>

</body>

</html>

关于javascript - 复选框一开始就不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61897389/

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