2009-09-02 55 views
10

我有一個問題,我有X <input type="checkbox" />在我的代碼中,現在我想foreach這個對象/數組它的輸出。 - 看我的代碼。foreach對象/數組在jQuery中

$("#denied_seekrs").click(function() 
{ 
    if (!isCheckedById("selectname")) 
    { 
     alert ("Please select at least one event"); 
     return false; 
    } 
    else 
    { 
     alert($("input[@id=selectname]:checked").val()); //submit the form 
    } 
}); 

function isCheckedById(id) 
{ 
    var checked = $("input[@id="+id+"]:checked").length; 
    if (checked == 0) 
    { 
     return false; 
    } 
    else 
    { 
     return true; 
    } 
} 

當我將它輸出在警告我碰到一個對象,但如果我有選擇2複選框,我什麼這2個複選框的值。

我希望我能是有益的,都在這裏認識我:)

+3

@NeoNmaN:有許多可用於幾乎所有的瀏覽器有用的拼寫檢查程序。不要誤解我的意思,但我認爲你應該安裝一個。這個問題真的很難理解。 – Tomalak 2009-09-02 09:27:40

+0

你必須澄清你的問題。你想從所有選中的框中獲取價值?你想用它做什麼?當你提交表單時,就像你在提醒旁邊的評論中提到的那樣,所有的複選框值都將被髮送到服務器。 – 2009-09-02 09:31:21

回答

25

如何

$("#denied_seekrs").click(function() { 
    var checkedInputs = $("input:checked"); 
    var test = ""; 
    $.each(checkedInputs, function(i, val) { 
     test += val.value+","; 
    }); 
    test = test.substring(0,(test.length-1)); 
    alert(test); 
}); 
+1

爲了使它適用於字符串數組,我改變了這一行:test + = val.value +「,」;測試+ = val +「,」; – 2011-07-24 03:06:04

0

當我到了你的權利,你希望用戶選擇一個複選框(或者是一個或多個?)。這應做到:

$("#denied_seekrs").click(function() 
{ 

    var $checkedInputs = $("input:checked"); 

    if ($checkedInputs.length != 1) 
    { 
     alert ("Please select one event"); 
     return false; 
    } 

    alert($checkedInputs.val()); //submit the form 
}); 

編輯: 再次閱讀您的問題後,我意識到,上面的代碼不會回答你的問題。但是,上述工作並且是您的解決方案的更短版本。也許你想用它來代替。要回答你的問題,你可以提醒所有的檢查,而像這樣的值:

更改此:

alert($checkedInputs.val()); //submit the form 

這樣:

var values = ""; 
    $checkedInputs.each(function(){ 
     values += $(this).val() + " "; 
    }); 

    alert(values); 
+0

沒有它的不工作,:/它只返回第一個值。 – ParisNakitaKejser 2009-09-02 09:34:43

+0

@NeoNmaN:是的,我首先把你的問題弄錯了。我剛剛添加了一個示例來獲取警報中的所有值。 – 2009-09-02 09:42:09

3

我不完全相信你」重新尋找,但我猜jQuery.each() method將有所幫助。您可以使用它遍歷數組,對象等。

var arr = [ "one", "two", "three", "four", "five" ]; 

jQuery.each(arr, function() { 
    $("#" + this).text("My id is " + this + "."); 
    return (this != "four"); // will stop running to skip "five" 
}); 
2

怎麼是這樣的:

jQuery.each(checked, function() { 
     $(checked + this).text("My id is " + this + "."); 

    }); 
+0

http://docs.jquery.com/Utilities/jQuery.each – william 2009-09-02 09:26:00

+0

mabye你應該重新命名爲另一個名字 – william 2009-09-02 09:27:00

2

它可以是 - 最終 - 你正在尋找$.serializeArray()$.serialize()

如果沒有,那麼也許這是幫助你:

$("#denied_seekrs").click(function() 
{ 
    if (!isCheckedById("selectname")) 
    { 
     alert ("Please select at least one event"); 
     return false; 
    } 
    else 
    { 
     // prepare array of values 
     var values = []; 

     // prepare list of checked checkboxes 
     var $checkboxes = $("input[@id=selectname]:checked"); 

     // push each individual value into the array 
     $checkboxes.each(function() { values.push($(this).val()); }); 

     // debug output 
     alert(values.join("\n")); 
     //submit the form 
    } 
});