2010-08-09 58 views
0

我想用一個變量替換硬編碼的字符串「Categories」。我怎樣才能做到這一點? 而不是始終有類別,類別可能會被變量ID,var id持有的值替換。如何用變量替換硬編碼值?

$(this).closest('ul').append('<li><input type="hidden" name="Categories.Index" value=' + newValue + ' /><input type="text" value="" name="Categories[' + newValue + '].Name" style="width:280px"/><input type="hidden" value="" name="Categories[' + newValue + '].ID" style="width:280px"/><input type="button" value= "Add" /> </li>'); 
+0

我不明白你的意思。你已經在使用變量了 - 你的問題是什麼? – 2010-08-09 09:06:55

+0

爲什麼你不編輯http://stackoverflow.com/questions/3437788/code-no-longer-working? – kennytm 2010-08-09 09:07:44

回答

4
var id = 'Categories'; 
$(this).closest('ul').append('<li><input type="hidden" name="' + id + '.Index" value=' + newValue + ' /><input type="text" value="" name="' + id + '[' + newValue + '].Name" style="width:280px"/><input type="hidden" value="" name="' + id + '[' + newValue + '].ID" style="width:280px"/><input type="button" value= "Add" /> </li>'); 
0

只是Concat的它,你已經做NEWVALUE以同樣的方式:

name="' + id + '[' + newValue + '].ID" 

執行相同的類別的每一次出現。