2012-12-13 89 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