在Struts2

时间:2015-06-24 06:35:46

标签: jsp struts2

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

enter image description here

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

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;
}

在execute()

    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中执行此操作。

2 个答案:

答案 0 :(得分:0)

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

在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);   
   }
   });

还有其他解决方案吗?

答案 1 :(得分:0)

你可以这样做

<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>