目前我有一個功能正常的代碼,用於在我的頁面上動態添加和刪除表單輸入。我有多個表單需要包含在頁面中,因此我做了一個事件操作,他們可以按下按鈕,並隱藏除相關表單之外的所有表單。這工作得很好,但它創建了與我的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>
做得好用於向DRY原則前進,但我可以問你一個演示張貼到[JS小提琴(http://jsfiddle.net/),或類似,所以我們可以很容易地看到發生了什麼並進行更改? – 2012-08-08 23:16:56
我從來沒有使用JS小提琴,但我認爲這應該是這樣的: http://jsfiddle.net/Sz8JQ/1/ – user1562781 2012-08-08 23:25:53