简体   繁体   English

使用jquery获取多个选择框的值

[英]getting values of multiple select boxes using jquery each

Hi i have a shop where you need to specify options before u can add something to a cart. 嗨,我有一个商店,你需要指定选项,然后才能添加东西到购物车。 I used to use radio buttons to add those specs but the list was getting to long so i rebuild it to select boxes. 我曾经使用单选按钮添加这些规格,但列表变得很长,所以我重建它以选择框。 The problem is that now sometimes the specs are added and sometimes it dont... 问题是,现在有时会添加规格,有时它不会......

The select boxes are populated in a loop. 选择框以循环方式填充。 and is done by smarty and looks like this 并且由smarty完成并且看起来像这样

        {**************SELECT BOX*******************}
        {if $menuaddonslist[add].mainaddonsname neq '' && $menuaddonslist[add].mainaddonsnamecnt >= 1}
            {$objSearchDetails->menuSubAddonsList($menuaddonslist[add].addonparentid,$menuaddonslist[add].menuaddons_menuid)}
                <div class="single">
                <span class="addonTitle">{$menuaddonslist[add].mainaddonsname|stripslashes}</span>
                <select class="popNameInput" id="selectBox_{$menuaddonslist[add].addonparentid}" name="addonstype_{$menuaddonslist[add].mainaddonsname}" style="border: 1px solid #dedede;margin-bottom:15px;width:230px;">
                {section name="subadd" loop=$menuSubaddonslist}
                    {if $menuSubaddonslist[subadd].subaddonsname neq ''}
                        {if $menuaddonslist[add].mainaddonsnamecnt eq '1'}
                        <option value="{$menuSubaddonslist[subadd].menuaddons_id}">
                          {$menuSubaddonslist[subadd].subaddonsname|ucfirst|stripslashes}
                            {if $menuSubaddonslist[subadd].menuaddons_priceoption eq 'Paid'} 
                            (+&nbsp;&#8364;{$menuSubaddonslist[subadd].menuaddons_price} )
                            {/if}
                        </option>
                        {/if}
                     {/if}
                {/section}
                </select>
                <input type="hidden" name="singleopt" class="singleopt" id="singleopt" value="single">
                </div>
                <div style="clear:both"></div>

and this is tje jquery 这是jjeery

            if(singleoption == 'single'){
                var AddonstypeSingle=[];
                $("div.single :option").each( function() {
                    AddonstypeSingle.push( this.value );
                });
                //alert(AddonstypeSingle);
            }

So if there are 10 select boxes it shows 10 times a div with class single. 因此,如果有10个选择框,它会显示10次div和class single。 and then I loop that and take the :option var but sometimes it just dont work. 然后我循环它并采取:选项var但有时它只是不工作。 before it was with radio options and the jquery was like this: 在使用无线电选项之前,jquery是这样的:

            if(singleoption == 'single'){
                var AddonstypeSingle=[];
                $("div.single input[type=radio]:checked").each( function() {
                    AddonstypeSingle.push( this.value );
                });
                //alert(AddonstypeSingle);
            }

That worked fine. 这工作得很好。 I can give each box a uniqe ID also, but the problem is that this list of select boxes is created automaticly and i can not hard code this in jquery. 我也可以给每个盒子一个uniqe ID,但问题是这个选择框列表是自动创建的,我不能在jquery中硬编码。

Is my selector wrong? 我的选择器错了吗?

You could try 你可以试试

if(singleoption == 'single'){
    var AddonstypeSingle=[];
    $("div.single select").each( function() {
        AddonstypeSingle.push( this.value );
    });
}

This will do the trick ;) 这将做的伎俩;)

var AddonstypeSingle=[];
$("div.single option:selected").each(function(){
    AddonstypeSingle.push($(this).val());
});

The selector to get all the options is: 获取所有选项的选择器是:

$("div.single option").each( function (){} );

The selector to get all the selected options is: 获取所有选定选项的选择器是:

$("div.single option:selected").each( function (){} );

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

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