2010-08-09 30 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" 

执行相同的类别的每一次出现。