简体   繁体   English

表单中字段的2个名称属性

[英]2 name attributes for a field in a form

I have a script that calculates the values in each and shows the calulated values. 我有一个脚本,可以计算每个脚本中的值并显示计算出的值。 At the end it also calculates the already calculated values from all div's 最后,它还会计算所有div的已计算值

Here is the html code: 这是html代码:

<td>
<div>
<input name="r" class="rate" type="text" maxlength="255" size="5" value />
<input name="p" class="pack" type="text" maxlength="255" size="5" value />
<span class="amount"></span>
</div>
</td>

<td>
<div>
<input name="r" class="rate" type="text" maxlength="255" size="5" value />
<input name="p" class="pack" type="text" maxlength="255" size="5" value />
<span class="amount"></span>
</div>
</td>

The problem is that I want to put all fields in a form and then submit them to a database. 问题是我想将所有字段放入表单中,然后将其提交到数据库。 However, all divs contain two input fields with name "r" and "p". 但是,所有div都包含两个名称分别为“ r”和“ p”的输入字段。

So, I am kind of stuck here because I cannot figure out how to make the names unique or how to have them passed to the DB using POST. 因此,我有点卡在这里,因为我无法弄清楚如何使名称唯一或如何使用POST将其传递给数据库。

This is what the calculating script looks like: 计算脚本如下所示:

<script type="text/javascript">//<![CDATA[ 

//any time the amount changes
$(document).ready(function() {
    $('input[name=r],input[name=p]').change(function(e) {
        var total = 0;
        var $row = $(this).parent();
        var rate = $row.find('input[name=r]').val();
        var pack = $row.find('input[name=p]').val();

        total = parseFloat(rate * pack);
        //update the row total
        $row.find('.amount').text(total);

        var total_amount = 0;
        $('.amount').each(function() {
            //Get the value
            var am= $(this).text();
            console.log(am);
            //if it's a number add it to the total
            if (IsNumeric(am)) {
                total_amount += parseFloat(am, 10);
            }
        });
        $('.total_amount').text(total_amount);
    });
});

//isNumeric function Stolen from: 
//http://stackoverflow.com/questions/18082/validate-numbers-in-javascript-isnumeric

function IsNumeric(input) {
    return (input - 0) == input && input.length > 0;
}
//]]>  

</script>

HTML: HTML:

<input type="text" name="r[]">
<input type="text" name="p[]">
<hr>
<input type="text" name="r[]">
<input type="text" name="p[]">
<hr>
<input type="text" name="r[]">
<input type="text" name="p[]">

PHP: PHP:

for ($i = 0; $i < count($_POST['p']); $i++) {
    $rate = $_POST['r'][$i];
    $pack = $_POST['p'][$i];
    // do something with $rate and $pack
}

Since the browser submits all inputs (even if no value has been entered) and by specification it submits them in the order they are defined in the HTML code, you can rely that the elements in the two $_POST arrays will line up and the corresponding rate and pack will be received at the same index in the respective array. 由于浏览器会提交所有输入(即使没有输入任何值),并且按照规范它会按照HTML代码中定义的顺序提交它们,因此您可以依靠两个$_POST数组中的元素将$_POST ,并且相应的ratepack将在相应数组中的相同索引处接收。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM