2012-08-08 62 views
3

目前我有一個功能正常的代碼,用於在我的頁面上動態添加和刪除表單輸入。我有多個表單需要包含在頁面中,因此我做了一個事件操作,他們可以按下按鈕,並隱藏除相關表單之外的所有表單。這工作得很好,但它創建了與我的jQuery/JavaScript代碼衝突,因爲代碼使用類名來動態添加或刪除輸入字段。這兩種形式都必須在同一個類名下才能使用jQuery函數,但這會產生衝突,並且該函數將停止工作。我可以只寫兩個版本的函數(每個類都有一個版本),但我試圖找到一種方法來概括這個,這樣我就不必有太多的函數。我在想這樣做這樣的事情:推廣jQuery動態添加/刪除多個表單的表單輸入

$('.ccinput').addClass('dinput').removeClass('ccinput'); 

在那裏我會相應地改變每個窗體的類名,這樣他們將與jQuery函數傳遞唯一的。這種方法似乎非常容易出錯,尤其是對於2個以上的表格(我計劃總共有4個表格)。你們知道另一種我可以做到的方式嗎?這裏是我的HTML代碼作爲參考:

編輯:我已經對代碼進行了重大更改,所以這裏是新版本。我刪除了表單輸入中的所有ID值,並更改了jQuery函數,以便它不使用ID值作爲選擇器。這消除了一些衝突。我正在嘗試使用attr('class','newclass'),以便jQuery函數適用於這兩種代碼。它似乎對第一種形式完美地起作用,但它拒絕爲第二種形式發揮作用。我不知道爲什麼。

<html> 

<head> 
<title></title> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> 
<script src="jquery.maskedinput.js" type="text/javascript"></script> 

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#table1").hide(); 
     $("#table2").hide(); 
     $("#cc_button").click(function(){ 
      $("#table1").show(); 
      $("#table2").hide(); 
      $("#cctable tr").attr('class', 'dinput'); 
      $("#dbtable tr").attr('class', 'dbinput'); 
     }); 
     $("#db_button").click(function(){ 
      $("#table2").show(); 
      $("#table1").hide(); 
      $("#dbtable tr").attr('class', 'dinput'); 
      $("#cctable tr").attr('class', 'ccinput'); 
     }); 
     $('#btnAdd').click(function() { 
      var num  = $('.dinput').length; // how many "duplicatable" input fields we currently have 
      var newNum = new Number(num + 1);  // the numeric ID of the new input field being added 

      // create the new element via clone(), and manipulate it's ID using newNum value 
      var newElem = $('.dinput:last').clone(); 

      // insert the new element after the last "duplicatable" input field 
      $('.dinput:last').after(newElem); 
      $(".dinput:last td:first").replaceWith("<td>" + newNum + "</td>"); 

      // enable the "remove" button 
      $('#btnDel').attr('disabled',''); 

      $(".date").mask("99/99/9999"); 

      // business rule: you can only add 20 names 
      if (newNum == 20) 
       $('#btnAdd').attr('disabled','disabled'); 
     }); 

     $('#btnDel').click(function() { 
      var num = $('.dinput').length; // how many "duplicatable" input fields we currently have 
      $('.dinput:last').remove();  // remove the last element 

      // enable the "add" button 
      $('#btnAdd').attr('disabled',''); 

      // if only one element remains, disable the "remove" button 
      if (num-1 == 1) 
       $('#btnDel').attr('disabled','disabled'); 
     }); 

     $(".date").mask("99/99/9999"); 
    }); 
</script> 
</head> 
<body> 
<div id="tablebuttons"> 
<button type="button" id="cc_button">CC</button> <button type="button" id="db_button">DB</button> 
</div> 
<div id="table1"> 

<form id="ccards" method="post" action="<?php echo htmlentities($PHP_SELF); ?>"> 
<table border="1" cellspacing="0"> 
<tr> 
<th></th> 
<th># (last four digits)</th> 
<th>Amount</th> 
<th>Approval</th> 
<th>Date Received</th> 
</tr> 
<tbody id ="cctable" > 
<tr class="ccinput"> 
    <td> 1 </td> 
    <td> <input type="text" name="cc_num[]" maxlength="4" /> </td> 
    <td> <input type="int" name="cc_amnt[]" /> </td> 
    <td> <input type="text" name="cc_app[]" maxlength="10" /> </td> 
    <td> <input class="date" type="text" name="cc_date[]" /> </td> 
</tr> 
</tbody> 
</table> 
<div> 
    <input type="button" id="btnAdd" value="Add CC" /> 
    <input type="button" id="btnDel" value="Remove CC" /> 
</div> 
<input type="submit" value="Submit" name="submit" /> 
</form> 
</div> 
<div id="table2"> 

<form id="db" method="post" action="<?php echo htmlentities($PHP_SELF); ?>"> 
<table border="1" cellspacing="0"> 
<tr> 
<th></th> 
<th>DB #</th> 
<th>Amount</th> 
<th>Date</th> 
</tr> 
<tbody id="dbtable"> 
<tr class="dbinput"> 
    <td> 1 </td> 
    <td> <input type="text" name="db_num[]" /> </td> 
    <td> <input type="int" name="db_amnt[]" /> </td> 
    <td> <input class="date" type="text" name="db_date[]" /> </td> 
</tr> 
</tbody> 
</table> 
<div> 
    <input type="button" id="btnAdd" value="Add DB" /> 
    <input type="button" id="btnDel" value="Remove DB" /> 
</div> 
<input type="submit" value="Submit" name="submit" /> 
</form> 
</div> 
</body> 

</html> 
+1

做得好用於向DRY原則前進,但我可以問你一個演示張貼到[JS小提琴(http://jsfiddle.net/),或類似,所以我們可以很容易地看到發生了什麼並進行更改? – 2012-08-08 23:16:56

+0

我從來沒有使用JS小提琴,但我認爲這應該是這樣的: http://jsfiddle.net/Sz8JQ/1/ – user1562781 2012-08-08 23:25:53

回答

0

好的,我解決了它。有我的選擇多問題,我不得不修復,但此後以下解決方案完美的作品:

$("#cc_button").click(function(){ 
    $("#table1").show(); 
    $("#table2").hide(); 
    $("#cctable tr").attr('class', 'dinput'); 
    $("#dbtable tr").attr('class', 'dbinput'); 
}); 
$("#db_button").click(function(){ 
    $("#table2").show(); 
    $("#table1").hide(); 
    $("#dbtable tr").attr('class', 'dinput'); 
    $("#cctable tr").attr('class', 'ccinput'); 
}); 

這基本上改變每個根據被按下按鈕表的類屬性。從理論上講,這應該適用於4種形式,儘管我還沒有嘗試過。這對於那些希望看到我做了什麼更新的代碼(因爲第一個代碼,我改變了很多):

<html> 

<head> 
<title></title> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script> 
<script src="jquery.maskedinput.js" type="text/javascript"></script> 

<script type="text/javascript"> 
    $(document).ready(function() { 
     $("#table1").hide(); 
     $("#table2").hide(); 
     $("#cc_button").click(function(){ 
      $("#table1").show(); 
      $("#table2").hide(); 
      $("#cctable tr").attr('class', 'dinput'); 
      $("#dbtable tr").attr('class', 'dbinput'); 
     }); 
     $("#db_button").click(function(){ 
      $("#table2").show(); 
      $("#table1").hide(); 
      $("#dbtable tr").attr('class', 'dinput'); 
      $("#cctable tr").attr('class', 'ccinput'); 
     }); 
     $('.btnAdd').click(function() { 
      var num  = $('.dinput').length; // how many "duplicatable" input fields we currently have 
      var newNum = new Number(num + 1);  // the numeric ID of the new input field being added 

      // create the new element via clone(), and manipulate it's ID using newNum value 
      var newElem = $('.dinput:last').clone(); 

      // insert the new element after the last "duplicatable" input field 
      $('.dinput:last').after(newElem); 
      $(".dinput:last td:first").replaceWith("<td>" + newNum + "</td>"); 

      // enable the "remove" button 
      $('.btnDel').attr('disabled',''); 

      $(".date").mask("99/99/9999"); 

      // business rule: you can only add 20 names 
      if (newNum == 20) 
       $('.btnAdd').attr('disabled','disabled'); 
     }); 

     $('.btnDel').click(function() { 
      var num = $('.dinput').length; // how many "duplicatable" input fields we currently have 
      $('.dinput:last').remove();  // remove the last element 

      // enable the "add" button 
      $('.btnAdd').attr('disabled',''); 

      // if only one element remains, disable the "remove" button 
      if (num-1 == 1) 
       $('.btnDel').attr('disabled','disabled'); 
     }); 

     $(".date").mask("99/99/9999"); 
    }); 
</script> 
</head> 
<body> 
<div id="tablebuttons"> 
<button type="button" id="cc_button">CC</button> <button type="button" id="db_button">DB</button> 
</div> 
<div id="table1"> 

<form id="ccards" method="post" action="<?php echo htmlentities($PHP_SELF); ?>"> 
<table border="1" cellspacing="0"> 
<tr> 
<th></th> 
<th># (last four digits)</th> 
<th>Amount</th> 
<th>Approval</th> 
<th>Date Received</th> 
</tr> 
<tbody id ="cctable" > 
<tr class="ccinput"> 
    <td> 1 </td> 
    <td> <input type="text" name="cc_num[]" maxlength="4" /> </td> 
    <td> <input type="int" name="cc_amnt[]" /> </td> 
    <td> <input type="text" name="cc_app[]" maxlength="10" /> </td> 
    <td> <input class="date" type="text" name="cc_date[]" /> </td> 
</tr> 
</tbody> 
</table> 
<div> 
    <input type="button" class="btnAdd" value="Add" /> 
    <input type="button" class="btnDel" value="Remove" /> 
</div> 
<input type="submit" value="Submit" name="submit" /> 
</form> 
</div> 
<div id="table2"> 

<form id="db" method="post" action="<?php echo htmlentities($PHP_SELF); ?>"> 
<table border="1" cellspacing="0"> 
<tr> 
<th></th> 
<th>DB #</th> 
<th>Amount</th> 
<th>Date</th> 
</tr> 
<tbody id="dbtable"> 
<tr class="dbinput"> 
    <td> 1 </td> 
    <td> <input type="text" name="db_num[]" /> </td> 
    <td> <input type="int" name="db_amnt[]" /> </td> 
    <td> <input class="date" type="text" name="db_date[]" /> </td> 
</tr> 
</tbody> 
</table> 
<div> 
    <input type="button" class="btnAdd" value="Add" /> 
    <input type="button" class="btnDel" value="Remove" /> 
</div> 
<input type="submit" value="Submit" name="submit" /> 
</form> 
</div> 
</body> 

</html> 
0

您可以使用這樣的事情。

//when the Add Field button is clicked 
$("#add").click(function (e) { 
//Append a new row of code to the "#items" div 
    $("#items").append('<div><input type="text" name="input[]"><button class="delete">Delete</button></div>'); 
}); 

的詳細教程http://voidtricks.com/jquery-add-remove-input-fields/

相關問題