我想顯示所有已檢查的元素。關於jquery
通過使用此代碼,我試圖將所有選中的元素推送到array1。
var array1 = new Array();
$(':checkbox').is(':checked').each(function()
{ //control not coming here
array1.push($(this));
}
);
alert("hi");
如何顯示array1內容。提前感謝。 注意:檢查意見
我想顯示所有已檢查的元素。關於jquery
通過使用此代碼,我試圖將所有選中的元素推送到array1。
var array1 = new Array();
$(':checkbox').is(':checked').each(function()
{ //control not coming here
array1.push($(this));
}
);
alert("hi");
如何顯示array1內容。提前感謝。 注意:檢查意見
toSource()可以幫助ü這樣做
var arraytest = array1.toSource()
alert(arraytest)
'toSource'是Firefox的擴展,它很可能在其他瀏覽器中不存在。 – 2011-05-09 10:05:36
你可以簡單地做:
var obj = $(":checkbox:checked");
...然後顯示obj
對象的內容。 jQuery實例非常像數組。他們有一個length
屬性和數字索引,它們將爲您提供該索引處的底層DOM元素。例如:
var obj = $(":checkbox:checked");
var n;
for (n = 0; n < obj.length; ++n) {
show(obj[n]); // `show` will receive ehe DOM element
}
你會經常看到上面這樣寫的,雖然:
$(":checkbox:checked").each(function() {
show(this);
});
...因爲jQuery實例有一個each
的功能,將調用你的回調爲每個元素的對象。更多the docs(儘管jquery.com今天有問題; here's a link到Google上的緩存版本)。
你想要顯示什麼?該數組包含jQuery對象,那麼輸出結果應該是什麼?實際上,不需要將它們推送到數組。取決於你想做什麼,你可以使用:var array1 = $(':checked');'(不是說使用':checked'是最好的方法)。 – 2011-05-09 10:02:32