简体   繁体   English

为复选框指定值(true或false)

[英]Assigning a value to the checkbox (true or false)

How do I set a value to the checkbox, true or false. 如何设置复选框的值,true或false。

<%    Boolean inactive = Boolean.valueOf(request.getParameter("isCheckedUnmatch"));
boolean isChecked = inactive.booleanValue();
%>

<script language = javascript>
function chgFlag(isChecked){
alert(isChecked);
isChecked = true;
}
</script>

<td align = "left">
<input name = "chkUnmatch" type = "checkbox" value = "<%=isChecked%>" onclick = "if (this.checked==true){chgFlag(<%=true%>)}">
<font face = "verdana" size = "2"><b>Unmatch Payment</b></font>
</td>

I have tried this code and even if I checked the box, the value of the boolean from the java controller is still false, its default value. 我已经尝试过这段代码,即使我选中了这个框,java控制器的布尔值仍然是false,它是默认值。

NOTE: I am using Java 1.3, the latest version of Java when the application was developed. 注意:我正在使用Java 1.3,这是开发应用程序时的最新版本的Java。

u may try this: 你可以试试这个:

<input type="checkbox" name="chkUnmatch" onclick="chgFlag(this.checked)" value="" id="box"/>

<script>
  function chgFlag(chk){
    var box = document.getElementById("box");
    box.value = chk;
  }
  <%
  Object tmp = request.getParameter("isCheckedUnmatch");
  if (null != tmp) {
    boolean mrk = Boolean.valueOf(tmp);
  %>
    chgFlag(<%=mrk%>);
  <%}%>
</script>

First of all, in JSP java codes and Scriptlets ( <% %> , <%! %> , <%= %> ), expression ${} etc.. all are processed before the HTML, javascripts and css are processed. 首先,在JSP java代码和Scriptlets( <% %><%! %><%= %> ),表达式${}等等都处理完HTML,javascripts和css之前。

even if I checked the box, the value of the boolean from the java controller is still false, its default value. 即使我选中了该框,java控制器中的布尔值仍为false,即默认值。

If you want to change the server side variable value (ie Controllers, Models, Servlets etc) on checked/unchecked of a checkbox, there is some ways: 如果要在选中/取消选中复选框时更改服务器端变量值(即控制器,模型,Servlet等),有以下几种方法:

1) Use a AJAX to send a request to server for variable value changes: 1)使用AJAX向服务器发送变量值更改请求:

HTML page like: HTML页面如:

<form id="myForm" action="servletUrlPattern" method="post">
    <input name="chkUnmatch"
        type="checkbox"
        onclick="if (this.checked==true){chgFlag(true)}">Check Box
    <input type="submit"/>
</form>
<script src="//code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript">
 function chgFlag(isChecked){
     console.log('isChecked: '+isChecked);
     $.post( $('#myForm').attr('action'), { valueToChange : isChecked }).done(function( data ) {
            alert( "Data Loaded: " + data );
      });
 }
</script>

and in Servlet: 并在Servlet中:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("doPost() invoked..");

    Boolean valueToChange = Boolean.valueOf(request.getParameter("valueToChange"));
    System.out.println("valueToChange:"+valueToChange);
    //Here, Do your works..
    response.getWriter().write("successfully changed.");        
}

2) 2)

Use form with submit button. 使用提交按钮的表单。


See also: jquery post doc 另见: jquery post doc

This should help. 这应该有所帮助。

EXSAMPLE EXSAMPLE

<html>
    <head>
        <script type="text/javascript">
            function refreshCheckboxValue(checkbox){
                checkbox.value = checkbox.checked;
            }
        </script>
    </head>
    <body>
        <input type="checkbox" onclick="refreshCheckboxValue(this)" value="false"/>
    </body>
</html>

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

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