【问题标题】:Create Multiple check boxes in Struts2在 Struts2 中创建多个复选框
【发布时间】:2015-06-25 12:50:26
【问题描述】:

我需要在 S2 中创建多个复选框,如下所示。

使用此代码我只得到一个复选框

public class EmployeeListBean {
private String empCode = null;
private String empName = null;
public EmployeeListBean(String empCode,String empName)
{
   //constructor
}
//setter and getter methods
}

在行动课上

public ArrayList<EmployeeListBean> getListOfEmployees()
{
    return listOfEmployees;
}

在执行()中

    listOfEmployees = new ArrayList<EmployeeListBean>();
    listOfEmployees.add(new EmployeeListBean("1", "Smith"));

在 JSP 中,

 <s:iterator value="listOfEmployees">
 <s:checkbox name="someselectedname" label="%{empName}"  fieldValue="%{empCode}"/><br/>
 </s:iterator>

我按照正常的方式。这里我想在三个复选框中只选择一个复选框。所以我们可以在 JS 或 Jquery 中完成。在 HTML 中,就像

  <label>abc:&nbsp;</label>
  <input type="checkbox" name="ballet" />
  <input type="checkbox" name="ballet" />
  <input type="checkbox" name="ballet" />
  <br/>
  <label>def:&nbsp;</label>
  <input type="checkbox" name="ballet1" />
  <input type="checkbox" name="ballet1" />
  <input type="checkbox" name="ballet1" />

但我想要以上格式。如何在 S2 中执行此操作。

【问题讨论】:

  • 如果您只想选择三个复选框中的一个,那么我建议使用单选按钮而不是复选框。

标签: jsp struts2


【解决方案1】:

我为此找到了一种解决方案。根据我的需要,我需要更改一些字段值以获取相应的复选框值。

在 JSP 中:

   <s:iterator value="listOfEmployees">
   <s:property value="%{empName}" />&nbsp;
   <s:checkbox name="someselectedname%{empCode}" fieldValue="%{empCode}" theme="simple"/>&nbsp;
   <s:checkbox name="someselectedname%{empCode}" fieldValue="%{empCode}" theme="simple"/>&nbsp;
   <s:checkbox name="someselectedname%{empCode}" fieldValue="%{empCode}" theme="simple"/><br/>
   </s:iterator>

在 JS 中:

   $(':checkbox').on('change',function(){
   var th = $(this), name = th.prop('name'); 
   if(th.is(':checked')){
   $(':checkbox[name="'  + name + '"]').not($(this)).prop('checked',false);   
   }
   });

还有其他解决办法吗?

【讨论】:

    【解决方案2】:

    你可以这样做

    <s:iterator value="listOfEmployees">
      <input type="checkbox" name="vehicle" value="Bike"> I have a bike
    <input type="checkbox" name="vehicle" value="Car"> I have a car
    <input type="checkbox" name="vehicle" value="Bus"> I have a bus<br>
     </s:iterator>
    

    【讨论】:

    • 通过使用此代码,可以在四个复选框中选择两个复选框。我只想要一个@xrcwrn。
    • 如果你只想选择一个然后使用重做按钮。
    猜你喜欢
    • 1970-01-01
    • 2019-11-19
    • 2011-10-05
    • 1970-01-01
    • 2018-11-17
    • 2014-05-20
    • 1970-01-01
    • 2022-01-06
    • 2023-03-15
    相关资源
    最近更新 更多