[英]Second occurrence of checkbox
如何獲得第二次出現的復選框? 這是我嘗試過的:
<div id="form">
<label for="first">First</label>
<input type="checkbox" name="first" value="1" >
<br />
<label for="second">Second</label>
<input type="checkbox" name="second" value="2" >
<br />
</div>
$('#form').find('input[type=checkbox]').eq(1)
但它對我不起作用。
您的選擇器不正確:
$('#form').find('input[type=checkbox]').eq(1)
應該:
$('#form').find('input[type="checkbox"]').eq(1)
你也可以這樣做(在這個jsfiddle證明):
jQuery('#form :checkbox[name="second"]:eq(1)');
(注意使用:checkbox
選擇器,在選擇器中包含eq(1)
並合並選擇器 - 表單選擇器, .find()
調用和.eq()
調用)
var foo = function(){
var inputs = document.getElementById('form').getElementsByTagName('input');
for(var i=0; i<inputs.length;i++){
var checkbox = inputs[i];
if(checkbox.type == 'checkbox' && checkbox.name == 'second'){
return checkbox;
}
}
};
在純JS中你可以做這樣的事情:)
我認為你應該改變第二個復選框的名稱
給每個復選框一個id並執行類似的操作
var $checkBox = $("#checkboxid");
$('#form')。find('input [type = checkbox]')。eq(1)選擇項目。 之后你必須做點什么。 例如:
$('#form').find('input[type=checkbox]').eq(1).val('5'); //Changes the value to 5
$('#form').find('input[type=checkbox]').eq(1).hide(); //hides it
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.