相同表单中的三个或更多字段 - 如何动态添加/删除空白字段

时间:2014-08-08 15:30:55

标签: javascript jquery forms

我在同一个表单中有三个单独的字段。我希望能够为每个字段动态添加/删除空白字段。

这里是字段和JQuery段 - 它确实适用于第一个字段但不适用于其他字段。我需要做什么?我也尝试将第二个.append语句放入第一个语句中,但也没有用。

我还向第二个警报发出警报,看它是否会触发。确实如此,但按钮调用不起作用。

如果我可以让前两个字段起作用,我该如何处理第三个字段?就像我说的那样,第一个工作正常。

字段

<div class="col-sm-3" id="submitterEmail">  
        Email<g:field type="email" name="submitterEmail" class="form-control" required="" value="" aria-labelledby="submitterEmail-label"/><button id="add">Add+</button>
</div>
<div class="col-sm-2">  
        Fax<g:field type="text" name="submitterFax" class="form-control" required="true" value="" aria-labelledby="submitterFax-label"/><button id="add2">Add+</button>
</div>
<div class="col-sm-5">
        Specimen<g:select name="specimen" from="" class="form-control" type="text" required="true" class="form-control" aria-labelledby="specimen-label"/>
</div>

JQuery的

$(document).ready(function(){
    //when the Add Filed button is clicked
    $("#add").click(function (e) {
        //Append a new row of code to the div
        $("#submitterEmail").append('<div><g:field type="email" name="submitterEmail" class="form-control" required="" value="" aria-labelledby="submitterEmail-label"/><button class="delete">Delete</button></div>');
    });
    $("body").on("click", ".delete", function (e) {
        $(this).parent("div").remove();
    });
});    
$(document).ready(function(){
    //when the Add Filed button is clicked
    $("#add2").click(function (e) {
        //Append a new row of code to the div
        $("#submitterFax").append('<div><g:field type="text" name="submitterFax" class="form-control" required="" value="" aria-labelledby="submitterFax-label"/><button class="delete">Delete</button></div>');    
        alert('this is an alert test)       
    });    
});    
});

2 个答案:

答案 0 :(得分:0)

试试这个会起作用

<?php ?>// you can remove this tag.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    //when the Add Filed button is clicked
    $("#add").click(function (e) {
        //Append a new row of code to the div
        $("#submitterEmail").append('<div><input type="text" name="submitterEmail" class="form-control" required="" value="" aria-labelledby="submitterEmail-label"/><button class="delete">Delete</button></div>');
    });
    $("body").on("click", ".delete", function (e) {
        $(this).parent("div").remove();
    });
});    
$(document).ready(function(){
    //when the Add Filed button is clicked
    $("#add2").click(function (e) {
        //Append a new row of code to the div
        $("#submitterFax").append('<div><input type="text" name="submitterFax" class="form-control" required="" value="" aria-labelledby="submitterFax-label"/><button class="delete">Delete</button></div>');    

    });  

$("body").on("click", ".delete", function (e) {
        $(this).parent("div").remove();
    });  
});      

</script>

Html部分。

<div class="col-sm-3" id="submitterEmail">  
        Email<g:field type="email" name="submitterEmail" class="form-control" required="" value="" aria-labelledby="submitterEmail-label"/><button id="add">Add+</button>
</div>
<div class="col-sm-2" id="submitterFax">  
        Fax<g:field type="text" name="submitterFax" class="form-control" required="true" value="" aria-labelledby="submitterFax-label"/><button id="add2">Add+</button>
</div>
<div class="col-sm-5">
        Specimen<g:select name="specimen" from="" class="form-control" type="text" required="true" class="form-control" aria-labelledby="specimen-label"/>
</div>

答案 1 :(得分:0)

我办公室的一位同事也提出了这个替代修复,如果有人感兴趣的话......或许可能会涉及更多一些,但也可以很好地使用一些Bootstrap元素和格式。如果它对某人有帮助(它帮助了我!),那么我很高兴分享它。

var counter = 0;

function addContactEmail(){
    $("#submitterEmailTbody").append('<tr id="submitterEmail'+counter+'Row"><td><input type="text" name="submitterEmail" class="form-control" required=""/></td><td><button type="button" class="btn btn-danger" onclick="deleteContactEmail('+counter+')" onkeypress="deleteContactEmail('+counter+'); return false"><span class="glyphicon glyphicon-minus"></span></button></td></tr>');
    counter++;
}
function deleteContactEmail(id){
    $("#submitterEmail"+id+"Row").remove();
}

function addContactFax(){
    $("#submitterFaxTbody").append('<tr id="submitterFax'+counter+'Row"><td><input type="text" name="submitterFax" class="form-control" required=""/></td><td><button type="button" class="btn btn-danger" onclick="deleteContactFax('+counter+')" onkeypress="deleteContactFax('+counter+'); return false"><span class="glyphicon glyphicon-minus"></span></button></td></tr>');
    counter++;
}
function deleteContactFax(id){
    $("#submitterFax"+id+"Row").remove();
}