How to make mutex checkbox in Struts2?

73 views Asked by At

I have a variable paramMap, the type is HashMap<String, String>, which has two elements:

{"1", "areaNo"}
{"2", "areaOrgNo"}

and the checkbox code of Struts2 is as follows:

<s:iterator value="paramMap" var="params" status="paramStatus">
  <input type="checkbox" name="params" key_id="#paramStatus.index" value="<s:property value="#param.key"/>"
  <s:if test="#paramStatus.index=0">
    onclick=checkAreaNo()
  </s:if>

The logic I want is when I check areaNo, areaOrgNo will be unchecked, how should I write the function checkAreaNo() within the <script></script>? I have written checkAreaNo() as follows, but it doesn't work:

<script>
  function checkAreaNo() {
    if ( $(input[key_id='1']).prop("checked") ) {
      $(input[key_id='1']).prop("checked", false)
    }
  }
</script>
1

There are 1 answers

0
Roman C On

I don't know what elements are in your map, but in plain HTML elements are DOM and you can't use OGNL syntax with them. See this answer for details.

So, as I said in the commented answer to use OGNL to the plain html tag you should use <s:property> tag or JSP EL.

<input type="checkbox" name="params" key_id="<s:property value='#paramStatus.index'/>" value="<s:property value='#params.key'/>" label="<s:property value='#params.value'/>">

or

<input type="checkbox" name="params" key_id="${paramStatus.index}" value="${params.key}" label="${params.value}">