简体   繁体   English

选中复选框时如何获取多个值

[英]how to get multiple values when checkbox is checked

I'm trying to include checkbox with the list.Using that code I've included checkbox successfully.But I can not get the multiple values by using that.If I checked three checboxes, I can get only one value from that list. 我试图在列表中包含复选框,使用该代码成功包含了复选框,但是我无法使用该复选框获得多个值。如果我选​​中了三个复选框,则只能从该列表中获得一个值。 please help me out to find out the reason why I can not get multiple values at the same time. 请帮助我找出无法同时获取多个值的原因。

 <div class="profile-input"  style="height:100px; width:200px; overflow:auto;">

                 <s:iterator value="cityList">
                    <input type="checkbox" name="cityNumber[]" id="cityNo"  value="<s:property value="cityId"/>" onchange="getcountrycode(false);" multiple="multiple"/><s:property value="cityName"/><br/>
                    </s:iterator>

                    </div>

First, if you have a Collection waiting on the Action, you need to point the index of each element by using IteratorStatus ; 首先,如果有一个Collection在等待操作,则需要使用IteratorStatus指向每个元素的索引;
also include city name in a label, generate univoque IDs, and remove multiple="multiple" that doesn't mean anything on checkboxes: 还可以在标签中包含城市名称,生成明确的ID,并删除复选框中没有任何意义的multiple="multiple"

<s:iterator value="cityList" stat="ctr">
    <input type = "checkbox" 
           name = "cityNumber[<s:property value="%{#ctr.index}"/>]" 
          value = "<s:property value="cityId"/>" 
             id = "cityNo<s:property value="%{#ctr.index}"/>"
    />

    <label for = "cityNo<s:property value="%{#ctr.index}"/>">
       <s:property value="cityName"/>
    </label>

    <br/>
</s:iterator>

Then, it would be better to use Struts UI tag <s:checkbox /> : 然后,最好使用Struts UI标签<s:checkbox />

<s:iterator value="cityList" stat="ctr">
    <s:checkbox name = "cityNumber[%{#ctr.index}]" 
          fieldValue = "cityId" 
               label = "cityName"
    />
    <br/>
</s:iterator>

Or even removing the iterator by using <s:checkboxlist/> like explained here . 甚至使用<s:checkboxlist/>删除迭代器, 如此处所述

Use jQuery map/get functions: 使用jQuery map / get函数:

$('#save_value').click(function () {
    var sel = $('input[type=checkbox]:checked').map(function (_, el) {
        return $(el).val();
    }).get();

    alert(sel);
});

See demo here.. http://jsfiddle.net/J3LNt/2/ 在此处查看演示。.http://jsfiddle.net/J3LNt/2/

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

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