简体   繁体   English

如何通过使用html dom获取javascript中的复选框检查值

[英]How to get checkbox cheked value in javascript by using html dom

Basically i have a list of checkbox which the data are retrieved from database and i stored it in vector to loop it. 基本上,我有一个复选框列表,可从数据库中检索数据,并将其存储在矢量中以使其循环播放。 Now I need to get the value those are selected(checked) and bring it to another jsp for some purpose, may i know how can i make it ? 现在我需要获取那些被选中(选中)的值,并出于某种目的将其带到另一个jsp,我可以知道如何实现它吗? i get "undefined" value when i alert in javascript. 当我在javascript中发出警报时,我得到“未定义”值。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks 谢谢

First.jsp First.jsp

<%
Vector vFruit_code        = new Vector();
Vector vFruit_descp       = new Vector();   

DB_FRUIT.makeConnection();
String SQL  = "SELECT * FROM TB_FRUIT WHERE TYPE='FC' WITH UR";
DB_FRUIT.executeQuery(SQL);
while(DB_FRUIT.getNextQuery())
{
    String FRUIT_CODE = DB_FRUIT.getColumnString("VALUE1");
    String FRUIT_DESCP= DB_FRUIT.getColumnString("VALUE2");

     vFruit_code.addElement(FRUIT_CODE);
     vFruit_descp.addElement(FRUIT_DESCP);
}
DB_FRUIT.takeDown();  
%>
<html>
<head>
<script language="Javascript">
 function fnCalulate()
 {

    document.getPremium.FRUIT_CODE.value    = document.mainform.FRUIT_CODE.value;
    document.getPremium.submit();

    // this part i i get undefined value when i try to alert
    alert(document.mainform.FRUIT_CODE.value);
 }
</script>
</head>
<body>
<form name="mainform" method="post" action="pop_fruit_route.jsp">
<table>
        <tr>
        <td>  
              <%
                for (int i=0;i<vFruit_code.size();i++)
                {
                      String sCODE = (String) vFruit_code.elementAt(i);
                      String sDESCP = (String) vFruit_descp.elementAt(i);
               %>
              <input name="FRUIT_CODE" type="checkbox" id="<%=sCODE%>" value="<%=sCODE%>" onclick="fnCalulate();"><%= sDESCP %>
          <%}%>
        </td>  
        </tr>
</table>
</form>
<form name="getPremium" method="post" action="home/calculation/calFruit.jsp">
<input type="hidden" name="FRUIT_CODE">
</form>
</body>
</html>

i need to get the value from the checkbox which the items are checked to calFruit.jsp 我需要从检查项目的复选框中获取值到calFruit.jsp

String[] ID = request.getParameterValues("FRUIT_CODE");

output 输出 在此处输入图片说明

With what you show, this will fail: 根据您的显示,这将失败:

document.getPremium.FRUIT_CODE.value    = document.mainform.FRUIT_CODE.value;

because there is no FRUIT_CODE in getPremium. 因为getPremium中没有FRUIT_CODE。

If you have multiple checkboxes, you can't say document.mainform.FRUIT_CODE.value ; 如果有多个复选框,则不能说document.mainform.FRUIT_CODE.value ; FRUIT_CODE will return an array of all your fruit input tags. FRUIT_CODE将返回所有水果输入标签的数组。 you can loop over them and get the value for one that is checked, like: 您可以遍历它们并获取已检查值的值,例如:

for (var i = 0; i < document.mainform.FRUIT_CODE.length; ++i ) {
    if (document.mainform.FRUIT_CODE[i].checked)
        document.getPremium.FRUIT_CODE.value = document.mainform.FRUIT_CODE[i].value;
}
alert(document.getPremium.FRUIT_CODE.value);

(though it isn't at all clear to me what you want to happen when multiple ones are checked) (尽管对我来说并不清楚,当选中多个复选框时,你想发生什么)

or pass your function the element that was clicked, by changing the call to be: 或通过将调用更改为以下内容,向您的函数传递被单击的元素:

onclick="fnCalculate(this)"

and the function to: 和功能,以:

function fnCalculate(clicked_element) {
    document.getPremium.FRUIT_CODE.value = clicked_element.value;
}

though either way you probably want to decide what should happen when a click unchecks a checkbox. 尽管您可能会想通过这两种方式来确定单击取消选中复选框后应如何处理。

Note that using a library such as jquery makes everything much easier. 请注意,使用诸如jquery之类的库会使一切变得容易得多。

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

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