[問題] javascript讀取陣列物件的問題

看板Ajax作者 (我就是我)時間16年前 (2009/08/19 11:05), 編輯推噓2(209)
留言11則, 5人參與, 最新討論串1/1
在下有一個問題 我在網頁上有許多個相同名稱的物件 例如: 1<input type="text" name="author_name[]" size="10"> 2<input type="text" name="author_name[]" size="10"> 3<input type="text" name="author_name[]" size="10"> 4<input type="text" name="author_name[]" size="10"> 5<input type="text" name="author_name[]" size="10"> 6<input type="text" name="author_name[]" size="10"> 7<input type="text" name="author_name[]" size="10"> 8<input type="text" name="author_name[]" size="10"> 9<input type="text" name="author_name[]" size="10"> 我後面會將資料用PHP存入 這個我會 但是我想要驗證他有沒有輸入 我用javascript 沒辦法讀取 不管用author_name它顯示undefined(沒javascript錯誤) 還是用author_name[0]它也顯示undefined(javascript的錯誤) 根本不行讀到裡面的值 請問有什麼方法可以讀到裡面的值 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 123.50.57.247

08/19 11:51, , 1F
document.getElementsByName
08/19 11:51, 1F

08/19 13:31, , 2F
你變數名稱這樣取? author_name[][0]?
08/19 13:31, 2F

08/19 14:03, , 3F
我也是過author_name[][0],但是會出現錯誤
08/19 14:03, 3F

08/19 14:03, , 4F
不能沒有東西
08/19 14:03, 4F

08/19 14:04, , 5F
[]不能沒有東西,要有數值在[]裡面
08/19 14:04, 5F

08/19 20:45, , 6F
這就是兩者衝突的地方了...javascript要有數值在[]裡才行
08/19 20:45, 6F

08/19 20:46, , 7F
但送form出去時卻又得要[]中沒有東西後端才能以陣列來接...
08/19 20:46, 7F

08/19 20:46, , 8F
我的變通法是每個 text 另外給它 id, js 裡就用 id 來存取
08/19 20:46, 8F

08/19 22:44, , 9F
沒人用過一樓的方法嗎?那應該是可行的耶...
08/19 22:44, 9F

08/20 11:39, , 10F
我終於找到解決的辦法了,用了好多種關鍵字,終於找到了
08/20 11:39, 10F

08/20 11:40, , 11F
關鍵字為php javascript 陣列
08/20 11:40, 11F
文章代碼(AID): #1AYsliHI (Ajax)
文章代碼(AID): #1AYsliHI (Ajax)