这是我在http://jsfiddle.net/d0okie0612/22cTn/上工作的小提琴
我写了一个函数getNewId(),我想把它放到我的空数组[]中,每次我点击添加位置链接,每个表单都应该有一个不同的随机ID
所以 name="locations[random_id_goes_here][street]" 现在它的 name="locations[][street]"
继承人的html
<div id="container">
<div id="top_form">
<form>
Street: <input class="street" name="[][street]" id="form_element" type="text"> <br><br>
City: <input class="city" name="[][city]" id="form_element" type="text">
<select>
<option value=" ">State</option>
<option value="ca">CA</option>
<option value="az">AZ</option>
<option value="de">DE</option>
</select>
Zip: <input name="[][zipcode]" type="text"><br /><br />
</form>
</div>
</div>
<br />
<a href="#" id="awsomeButton">+ Add Loction</a>
<br />
<br />
<button id="submit_btn">Submit</button>
这是jquery
var tpl = ""
+ "<div id='location_div_<%= id %>'><h1>My Location #<%= id %></h1></div>";
var newId = new Date().getTime();
var template = _.template(tpl);
var compiled = template({id: newId});
var addedForm = "<div id='added_form'>"
+ "<a href='#' class='close_btn'>x</a>"
+ "Street: <input name='locations[][street]' type='text'>"
+ "<br /><br />"
+ "City: <input name='locations[][city]' type='text'>"
+ "<select>"
+ "<option value=' '>State</option>"
+ "</select>"
+ "Zip: <input name='locations[][zipcode]' type='text'>"
+ "</div>"
function getNewId() {
var newId = new Date().getTime();
return newId;
}
$('#awsomeButton').on('click', function (e) {
$(addedForm).hide().appendTo('form').fadeIn('slow');
$(getNewId()).appendTo();
});
$('.close_btn').live('click', function (event) {
event.preventDefault();
$(this).parents('div#added_form:first').fadeOut('slow', function () {
$(this).remove();
});
});
所以基本上我只是想获得空数组中的数字任何帮助将不胜感激谢谢
我问这个问题很奇怪吗?