ここにhttp://jsfiddle.net/d0okie0612/22cTn/で作業しているフィドルimがあります
私は関数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();
});
});
だから基本的には空の配列の数字を取得しようとしているだけです助けていただければ幸いですありがとうございます
私はこの質問を奇妙にしていますか?