2

每次单击按钮时,我都需要将 name='0fName' 增加一个,以便 php 脚本看到一个唯一的名称并且不会覆盖前一项。

$(document).ready(function(){
    $('#addMember').click(function() {
        for (var i = 0; i <= 6; i++); 
        console.log("addMember was called");
        $("#addfamMember").append("<input type='text' name='0fName'>");
    return false;
});
4

4 回答 4

3

添加计数器:

$(document).ready(function(){
var count=0;
    $('#addMember').click(function() {
        console.log("addMember was called");
        $("#addfamMember").append("<input type='text' name='" + count + "fName'>");
        count++;
    return false;
});
于 2013-04-05T18:06:10.147 回答
2

Another approach could be changing a name attribute to fName[]:

$('#addMember').click(function () {
    $("#addfamMember").append("<input type='text' name='fName[]'>");
    return false;
});

In this case data will be sent as fName array which may be more convenient to deal with than with a bunch of separate parameters.

于 2013-04-05T18:11:55.173 回答
1

.index()也会这样做:

$('#addMember').click(function() {
    var idx = $(':input:last').index();
    $("#addfamMember").append("<input type='text' name='" + idx + "fName'>");
    return false;
});

小提琴

于 2013-04-05T18:17:13.833 回答
1

就像 epascarello 说的,添加一个计数器:

$(document).ready(function(){

var counter = 0;

$('#addMember').click(function() {
    console.log("addMember was called");
    $("#addfamMember").append("<input type='text' name='" + counter + "fName'>");

    counter++;
    return false;
});
于 2013-04-05T18:05:35.350 回答