2012-12-13 45 views
0

继承人的小提琴IM上http://jsfiddle.net/d0okie0612/22cTn/获得随机ID为空数组

进出口工作写了一个函数getNewId(),我想它变成我的空数组[]每次我点击添加位置链接,每个人应具有每形式的不同的随机ID

所以NAME = “位置[random_id_goes_here] [街道]” 现在它的名字= “位置[] [街道]”

继承人的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(); 
    }); 
}); 

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

上午我问这个问题奇怪的?

回答

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'); 
}); 

Fiddle is here

编辑

添加上述myArray.push(firstId);在两个地方。

+0

是获得我的getNewID号码到[]中的唯一方法吗?我对jQuery非常陌生,上面的许多代码都超出了我的头脑。我认为这会更简单 – user1502223

+0

上面的'addFirstId'部分只有当你想唯一标识用户在页面加载时看到的第一组表单域时才需要。如果基于当前日期的随机数以毫秒为单位就足够了,则不需要“随机数”。 – mccannf

+0

有没有方法可以将getNewId函数添加到我的点击事件处理函数中?并将它放入[] – user1502223