2010-01-15 65 views
0

我有一個正在上飛使用下方產生的PHP形式($theFeatures是一個多維數組):選擇窗體數組值使用jQuery

<?php 
foreach($theFeatures as $theKey => $theValues) { 
?> 
    <input type="text" value="<?php echo $theValues['Key']; ?>" name="theFeatures[<?php echo $theKey; ?>]['Key']" size="6" /> 
    <input type="text" value="<?php echo $theValues['Value']; ?>" name="theFeatures[<?php echo $theKey; ?>]['Value']" /> <br /> 
<?php 
} 
?> 

這應該創建一個看起來像這樣的兩個輸入框:

<input value="Type" name="theFeatures[4]['Key']" size="6" type="text" /> 
<input value="Bird Table" name="theFeatures[4]['Value']" type="text" /> 

我怎樣才能在jQuery的(在上面的例子[4])陣列的第一部分,所以我可以存儲作爲一個JavaScript變量,並用它其他地方在我的jQuery?

乾杯,

安迪

回答

3

你可以嘗試只存儲在輸入一個單獨的屬性值,然後檢索與jQuery該屬性。

添加新屬性輸入

<input type="text" value="<?php echo $theValues['Key']; ?>" name="theFeatures[<?php echo $theKey; ?>]['Key']" size="6" key="<?php echo $theKey; ?>"/> 

檢索屬性與jQuery

var key = $("input").attr("key"); 
+0

這可能是最簡單的方法,而且避免了必須解析字符串。 – Peter 2010-01-15 17:06:21

+0

或者你也可以使用rel =「」屬性。 – 2010-01-15 17:28:31

+0

謝謝,親切。這是一個天才解決方案! – Schodemeiss 2010-01-19 09:07:01

1

當然T B的建議可以工作。您還可以執行以下操作來獲取名稱中包含「Key」的所有輸入框,並獲取第一組方括號中的值。這將獲得包含Key的名稱的任何輸入對象。

$("input[name*=Key]").each(function() { 
    var name = $(this).attr("name"); 
    var key = name.substring(name.indexOf("[") + 1, name.indexOf("]"); 
    //do whatever is needed with key 
});