使用 javascript 获取最后添加的字段数


Get last number of added fields with javascript

我制作了一个表单,您可以通过单击按钮添加更多字段。每次单击按钮时,它都会创建一个新字段,其后面的数字更高。我用jquery这样做,我需要用php获得最高的数字。因此,如果我有auto_part1和auto_part2我需要用 PHP 获得 2

这是我的 html:

<div id="parts">
    Part
    <input type="text" id="auto_part" name="auto_part" />
                <br />
    Description
    <input type="text" id="auto_description" name="auto_description" />
                <br />
</div>
    <a href="#" id="addField">Add another part</a>

jQuery 函数:

$(function() {
var scntDiv = $('#parts');
var i = $('#parts input').size() + -1;
$('#addField').on('click', function() {
    $('<br /><span>Part</span> <input type="text" id="auto_part'+i+'" name="auto_part'+i+'" /><br />').appendTo(scntDiv);
    $('<span>Description</span> <input type="text" id="auto_description'+i+'" name="auto_description'+i+'" /> <br />').appendTo(scntDiv);
    i++;
    return false;
});
$('#remScnt').on('click', function() { 
    if( i > 2 ) {
            $(this).parents('p').remove();
            i--;
    }
    return false;
});
});

创建一个隐藏的输入字段:

<input type="hidden" id="row_count" name="row_count" value="">

onsubmit做到:

$('#row_count').val(i);
如果我

没错,请也使用堆栈溢出搜索..

在这里你可以找到一种方法..

在同一页面上将jQuery变量传递给PHP?

Presumaby 这是最终提交的形式吗? 如果是这样,那么每次添加新零件字段时都有一个隐藏字段递增怎么样? 然后当它被发送时,PHP可以抓住它。

添加一个

隐藏字段(<input type="hidden" name="added_counter" id="added_counter" value="0" />),使用递增/递减函数($('added_counter').val(i);)保持其值,然后在处理表单提交期间使用PHP获取隐藏字段的值。

.HTML:

<div id="parts">
    Part
    <input type="text" id="auto_part" name="auto_part" />
    <br />
    Description
    <input type="text" id="auto_description" name="auto_description" />
    <br />
</div>
<input type="hidden" name="added_counter" id="added_counter" value="0" />
<a href="#" id="addField">Add another part</a>

j查询:

$(function() {
    var scntDiv = $('#parts');
    var i = $('#parts input').size() + -1;
    $('#addField').on('click', function() {
        $('<br /><span>Part</span> <input type="text" id="auto_part'+i+'" name="auto_part'+i+'" /><br />').appendTo(scntDiv);
        $('<span>Description</span> <input type="text" id="auto_description'+i+'" name="auto_description'+i+'" /> <br />').appendTo(scntDiv);
        i++;
        $('added_counter').val(i);
        return false;
    });
    $('#remScnt').on('click', function() { 
        if( i > 2 ) {
                $(this).parents('p').remove();
                i--;
        }
        $('added_counter').val(i);
        return false;
    });
});

然后,当您将表单提交给 PHP 时,PHP 可以获取新added_counter隐藏字段的值。

计算已发布值的数量以获得最高数字。我将数据作为数组发布,但您不必那样做。

$('<br /><span>Part</span> <input type="text" id="auto_part'+i+'" name="parts['+i+'][name]" /><br />').appendTo(scntDiv);
$('<span>Description</span> <input type="text" id="auto_description'+i+'" name="parts['+i+'][description]" /> <br />').appendTo(scntDiv);

$parts=isset($_POST['parts'])?$_POST['parts']:array();
$highest_number=count($parts);

如果您更改了添加的输入字段的命名方式,该怎么办?而不是使用: auto_part + i用 auto_part[]

与auto_description相同,将其命名为 auto_description[]。这样,它们将被 php 视为数组,您需要做的就是迭代数组。这对你有用吗?

添加您的 html 表单:

<input type="hidden" id="count" value="0"/>

在 onclick 事件中添加您的 javascript:

$('#count').val(i);

然后,您可以在 php 中获取您要查找的值作为字段计数的值。