0

这是我在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();
    });
});

所以基本上我只是想获得空数组中的数字任何帮助将不胜感激谢谢

我问这个问题很奇怪吗?

4

1 回答 1

0

加载 HTML 时,将随机数添加到第一个表单。这需要class='zip'添加到 zip 输入中。我假设您还需要每个state选择的唯一名称:

addFirstId();

function addFirstId() {
    var randomnumber=Math.floor(Math.random()*100001);
    var firstId=(new Date().getTime())+randomnumber.toString();
    $(".street").attr("name","locations["+firstId+"][street]");
    $("select").attr("name","locations["+firstId+"][state]");
    $(".city").attr("name","locations["+firstId+"][city]");
    $(".zipcode").attr("name","locations["+firstId+"][zipcode]");
    myArray.push(firstId);
}

以下为每个新表单添加一个随机数:

function getNewId() {
    //Random number is date in milliseconds + another random number added
    var randomnumber=Math.floor(Math.random()*100001);
    var newId=(new Date().getTime())+randomnumber.toString();
    var customForm = addedForm.replace(/\[\]/g,"["+newId+"]");
    myArray.push(newId);
    return customForm;
}

$('#awsomeButton').on('click', function(e) {
    $(getNewId()).hide().appendTo('form').fadeIn('slow');
});

小提琴在这里

编辑

在上面的 2 个地方添加myArray.push(firstId);

于 2012-12-13T22:04:41.157 回答