为多种形式概括的jQuery动态添加/删除形式输入(Generalizing jQuery dyna

2019-09-21 01:02发布

目前,我有一种动态添加和我的网页上删除的形式输入的功能代码。 我有我需要包括在页面上多形式,所以我做了一个事件的行动,他们可以按下一个按钮,它会隐藏除有关的一个所有形式。 这工作得很好,但它创造了我的jQuery / javascript代码,因为该代码使用一个类名来动态地添加或删除输入字段冲突。 这两种形式都必须是在相同的类名,我使用jQuery的功能是,但创建冲突和功能停止工作。 我可以只写功能(每个班)两个版本,但我试图找到一种方法来概括这一点,所以我不必有如此多的功能。 我想这样做是这样的:

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

在那里我会相应地改变每个窗体的类名,这样他们将与jQuery函数传递唯一的。 此方法似乎是非常容易出错,特别是超过2点的形式(我打算在具有4点形成总计)。 难道你们知道的另一种方式,我可以做到这一点? 这里是我参考的html代码:

编辑:我做的代码显著的变化,所以这里是新版本。 我删除从表单输入的所有ID值,并改变了jQuery函数,使得它不使用ID值作为选择器。 这移除了一些冲突。 而我现在正在试图使用ATTR(“类”,“的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>

Answer 1:

好吧,我解决了这个问题。 有我的选择多问题,我不得不修复,但此后以下解决方案完美的作品:

$("#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>


Answer 2:

您可以使用这样的事情。

//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/



文章来源: Generalizing jQuery dynamic add/remove form input for multiple forms