我一直在摔跤這個問題幾個小時了。jquery從表單數據創建數組
從本質上講,我需要做的就是採取以下或類似HTML:
<div id="excpdivs">
<div class="excpdiv" id="excpdiv0">
Date: <input name="excp[0][date]">
Open: <input name="excp[0][open]">
Close: <input name="excp[0][close]">
</div>
<div class="excpdiv" id="expdiv1">
Date: <input name="excp[1][date]">
Open: <input name="excp[1][open]">
Close: <input name="excp[1][close]">
</div>
,並得到類似通過AJAX以下PHP腳本的數組:
Array
(
[0] => Array
(
[date] => 2012-09-15
[open] => 3:00
[close] => 5:00
)
[1] => Array
(
[date] => 2012-09-16
[open] => 2:00
[close] => 5:00
)
)
我的主要問題是從輸入元素中獲取值。我的最新嘗試如下:
var results = [];
$(".excpdiv").each(function(){
var item = {};
var inpts = $(this).find("input");
item.date = $(inpts.get(0)).val();
item.open = $(inpts.get(1)).val();
item.close = $(inpts.get(2)).val();
results.push(item);
});
我在正確的軌道上,或我徹底失敗了?
標準形式提交,或阿賈克斯? (與你的問題無關,你可以說'inpts.eq(0).val()'而不是'$(inpts.get(0)).val()' - 你不必自己創建另一個jQuery對象。 – nnnnnn
你的問題是什麼?代碼可以改進,但可以。 – Christophe
nnnnnn,用Ajax代替標準表格提交 – AndrewStevens