gpt4 book ai didi

javascript - 如何使用外部按钮检查更改组复选框状态

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

我正在处理这个片段。如何更改 .change 点击时选中第二个复选框?

$(".change").on("click", function(){

});
body{
padding:30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="checkbox" autocomplete="off" checked> item 1
</label>
<label class="btn btn-primary">
<input type="checkbox" autocomplete="off"> item 2
</label>
<label class="btn btn-primary">
<input type="checkbox" autocomplete="off"> item 3
</label>
</div>
<br />
<br />

<button type="button" class="btn btn-default change">Item 2</button>

最佳答案

我希望我能正确理解你的问题。

$( '.change' ).on( 'click' , function(){
/* $( 'label' ).removeClass( 'active' ); */
$( 'label:nth-child(2)' ).addClass( 'active' );
/* $( 'input[type="checkbox"]:checked' ).prop( 'checked', false ); */
$( 'input[type="checkbox"]' )[ 1 ].checked = true;

console.log( 'Second item is checked: ' + $( 'input[type="checkbox"]' )[ 1 ].checked )
} );
body{
padding: 30px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>

<div class="btn-group" data-toggle="buttons">
<label class="btn btn-primary active">
<input type="checkbox" autocomplete="off" checked> item 1
</label>
<label class="btn btn-primary">
<input type="checkbox" autocomplete="off"> item 2
</label>
<label class="btn btn-primary">
<input type="checkbox" autocomplete="off"> item 3
</label>
</div>
<br />
<br />

<button type="button" class="btn btn-default change">Button</button>

关于javascript - 如何使用外部按钮检查更改组复选框状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50842069/

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