简体   繁体   English

如何使用jQuery计算选定的HTML选择元素

[英]How to count selected html select elements using jQuery

I've created form with adding new form elements after pressing Add new (or whatver) button, followed by: http://phpmysqlmania.blogspot.com/p/dynamic-table-row-inserter.html 在按下“添加新的(或其他)”按钮后,我添加了新的表单元素来创建表单,其后为: http : //phpmysqlmania.blogspot.com/p/dynamic-table-row-inserter.html

For example, I have: 例如,我有:

    <div id"row">
    <select id="singleorfamily1" style="width: 180px;" name="singleorfamily" >
    <option value="0">Select Single or Family</option>
    <option value="single">Single</option>
    <option value="family">Family</option>
    </select>
    <select id="selectedplan1" name="selectedplan" style="width: 110px;" >
<option value="0">Select Plan</option>
<option value="plan1">Plan1</option>
    <option value="plan2">Plan2</option>
</select>
    </div>

When I press Add new row, it add's new select element into row div: 当我按添加新行时,它将新的选择元素添加到行div中:

    <div id"row">
    <select id="singleorfamily1" style="width: 180px;" name="singleorfamily1" >
    <option value="0">Select Single or Family</option>
    <option value="single">Single</option>
    <option value="family">Family</option>
    </select>
    <select id="selectedplan1" name="selectedplan1" style="width: 110px;" >
<option value="0">Select Plan</option>
<option value="plan1">Plan1</option>
    <option value="plan2">Plan2</option>
</select>       
    <select id="singleorfamily2" style="width: 180px;" name="singleorfamily2" >
    <option value="0">Select Single or Family</option>
    <option value="single">Single</option>
    <option value="family">Family</option>
    </select>
    <select id="selectedplan2" name="selectedplan2" style="width: 110px;" >
<option value="0">Select Plan</option>
<option value="plan1">Plan1</option>
    <option value="plan2">Plan2</option>
</select>
    </div>

What I'm trying to make working (without success) and what I need to achieve - how to count selected Single and selected Family values in such case (select elements are dynamic - they can be 1 or 1000 or more). 我要努力工作的(没有成功的)和我需要实现的-在这种情况下如何计算选定的Single和Family系列值(选择元素是动态的-它们可以为1或1000或更多)。 Singles: X, Families: Y. They should be counted after 单打:X,家庭:Y。他们应在之后计算

jQuery('#singleorfamily'+i).change(function()

is made 被制造

I've tried "for" loop, "jQuery each"...but I can get it working just for 1 element, not for second, fifth, hundreths... I think I'm stucked how to correclty make looping... ;-O 我已经尝试过“ for”循环,“ jQuery each” ...但是我可以让它仅用于1个元素,而不是第二个,第五个,百分之一...我想我被困在如何纠正循环中。 。;-O

Any suggestions how to correctly process any "thing" (calculation, event) withing dynamically created elements? 有什么建议如何使用动态创建的元素正确处理任何“事物”(计算,事件)?

This what I've tried from suggestions: http://jsfiddle.net/AftpY/15/ 这是我根据建议尝试的内容: http : //jsfiddle.net/AftpY/15/

But the problem is - volume is not updated, it is updated just when 1 select element is changed, but it does not updates "on fly"... And there You can see that there is also other select element... 但是问题是-卷未更新,仅在更改1个select元素时才更新,但是它不会“即时”更新...在那里,您可以看到还有其他select元素...

Edited: Any ideas? 编辑:有什么想法吗? I'm really stucked with this problem -> I can not understand (see Jsfiddle link): *)why count info is not refresed after each singleorfamily1 selection, but just when I changed first occurance of singleorfamily1 (can not see/find the problem in the code); 我真的对这个问题感到困惑->我不明白(请参阅Jsfiddle链接):*)为什么在每次选择singleorfamily1后都无法重新获得计数信息,但是当我更改了singleorfamily1的首次出现时(看不到/发现问题)在代码中); *)how to count results of mixing selection values: how many singles from singleorfamily1 & plan1 from selectedplan1, how many family from selectedplan2 & plan2 from selectedplan2 etc... *)如何计算混合选择值的结果:selectorplan1中的singleorfamily1和plan1有多少单身,selectedplan2中的家庭有多少,selectedplan2中的plan2等...

TNX! TNX!

Use JQuery selector 使用JQuery选择器

    jQuery('#row select').on("change",function(){
        var singlecount = (jQuery("#row :selected[value='single']").length);
        var familycount = (jQuery("#row :selected[value='family']").length);
    });

http://jsfiddle.net/QPcjB/13/ http://jsfiddle.net/QPcjB/13/

This works for me: 这对我有用:

$('#row select').change(function () {
    var s = 0,
        f = 0;
    $('#row select').each(function () {
        if ($(this).val() == 'single') s++;
        if ($(this).val() == 'family') f++;
    })
    console.log(s, f)
})

jsFiddle example jsFiddle示例

Here's a simpler fiddle just showing how you can get the current sums at some point in time instead of a change event. 这是一个简单的小提琴,仅显示如何在某个时间点而不是更改事件获取当前总和。 http://jsfiddle.net/Aus2v/ http://jsfiddle.net/Aus2v/

//returns the number of selects with "single" chosen
$('select :selected[value="single"]').length; 

//returns the number of selects with "family" chosen
$('select :selected[value="family"]').length;

You can fire it on different event, but the main point is that you just check for selected option value like 您可以在其他事件上触发它,但要点是您只需检查所选的选项值,例如

 $('select option:selected').each(function(){
    if ($(this).val() == 'single'){
       i++;
    };
    if ($(this).val() == 'family'){
       k++;
    };
 });

Working Fiddle 工作小提琴

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

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