0

我正在尝试动态添加我能够做到的输入字段。但我不确定如何在输入字段中添加新名称。我希望它是一个递增的数字。所以第一组输入字段将是 input1 并且创建的每个克隆都会增加数字:input1、input2、input3 等。有谁知道我该怎么做?这是jsfiddle:http: //jsfiddle.net/liveandream/Xs7m8/

这是我的代码:

HTML:

<form action="#" method="post">
<div class="avail">
        <div class="roomType">
          <p>Tipo de Habitación:<br />
        <input type="text" name="roomType" /></p></div>
        <div class="roomType">
          <p><span class="small">Fecha de inicio:</span><br />
        <input type="text" name="Date1" /></p></div>
        <div class="roomType"><p><span class="small">Fecha de Termino:</span><br />
        <input type="text" name="Date2" /></p></div>
        <div class="roomType">
          <p>Tarifa:<br />
        <input type="text" name="roomRate" /></p></div><br clear="all" />
</div>
<input type="submit" value="+" id="copy" />
<input type="submit" value="Add to Database" name="add" />

还有我的jQuery:

$("#copy").click(function(e) {
       $(".avail").clone().removeAttr('id').insertBefore(this);
       e.preventDefault();
    }); 

提前感谢任何愿意提供帮助的人!!

4

1 回答 1

1

(删除了我最初的答案 - 忽略了 HTML 中的现有 ID)

您可以按房间对帖子结果进行分组。

results = {};
$('form').submit(function(e) {
    e.preventDefault(); // prevent automatic submitting

    $('.avail').each(function(index) {
        // Here your have the gather the fields and values in result array;

        results['room'+index] = result; // add array with results of one room to result
    });

    // Post the result
    $.post($(this).attr('action'), results, function(result) {
         // callback
    });
});
于 2012-04-05T15:08:34.807 回答