从同名多个选择框中获取值?包括jQuery和PHP的价值 [英] Get values from multiple select boxes with same name? Including its value with jquery and php

查看:104
本文介绍了从同名多个选择框中获取值?包括jQuery和PHP的价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里寻找答案,并没有找到任何直接可以帮助我的东西,所以我会在这里问。我有一个包含多个选择框的表单:

I have looked for answers on here and have not found anything that directly can help me so I am going to ask here. I have a form with multiple select boxes:

<select name="acabados[]">
<option value="1">ABC</option>
<option value="2">DEF</option>
<option value="3">GHI</option>
</select>

<select name="acabados[]">
<option value="1">ABC</option>
<option value="2">DEF</option>
<option value="3">GHI</option>
</select>

正如您所看到的,它们是完全相同的选择框。我已经告诉客户使用选择倍数,但他希望这样。用户可以根据需要添加尽可能多的选择框(因此一次最多可以选择20个选择框),我需要获取选项中的值(1或2或3)和文本。这是一个我需要分解的数组,因为我需要在php中添加选择框的总数值。我可以使用jquery收集值或php,这更容易。预先感谢您的帮助!

As you can see they are the exact same select box. I have told the client to use select multiple but he wants it this way. The user can add as many as these select boxes as they desire (so it could be up to 20 select boxes at a time) and I need to get the value (1 or 2 or 3) and the text inside the option. It is then an array that I need to break down cause I need to add the total values of the select boxes in php. I could either use jquery for collecting the values or php, which ever is easier. Thank you in advance for any help!!

推荐答案

试试这个,它也适用于动态创建的select元素。 >
$ b $

Try this, it will work for dynamically created select elements too.

$(document).on('change', 'select[name="acabados[]"]', function(){
    var total = 0;
    $('select[name="acabados[]"]').each(function(){
        total += parseInt($(this).val());
    });
});

total var将包含所有选择元素的选择值。

total var will contain the total of all the select elements selected value.

这篇关于从同名多个选择框中获取值?包括jQuery和PHP的价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆