将随机ID转换为空数组

时间:2012-12-13 20:09:50

标签: jquery

继承人正在研究http://jsfiddle.net/d0okie0612/22cTn/

我写了一个函数getNewId(),我希望每次点击添加位置链接时都将它放入我的空数组[]每个表格应该有一个不同的随机ID

所以name =“locations [random_id_goes_here] [街道]” 现在它的名字=“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();
    });
});

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

我问这个问题很奇怪吗?

1 个答案:

答案 0 :(得分:0)

在加载HTML时将随机数添加到第一个表单。这需要在zip输入中添加class='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');
});

Fiddle is here

修改

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