java - 需要一个在jsp中读取动态复选框ID的javascript函数

标签 java javascript jsp

我使用rs.getstring添加了一个动态复选框。我需要知道选中了哪个复选框。

<th>
  <input type="checkbox" name="chkSingle_<%=counter %>" id ="chkSingle_<%= counter%>" value="<%=rs.getInt(11)%>">
</th>

最佳答案

试试这个

function getCheckedItems(checkboxName) {
    var checkboxes = document.querySelectorAll('input[name="'+checkboxName+'"]:checked'), values = [];
    Array.prototype.forEach.call(checkboxes, function(el) {
        values.push(el.value);
    });
    return values;
}

您可以调用此函数并传递复选框的名称,您可以将 onclick 函数动态绑定(bind)到按钮并提供您在创建复选框时创建的动态名称。

已编辑

<!DOCTYPE html>
<html>
<head>

</head>
<body>
    <input type="checkbox" name="chkSingle" id ="chkSingle1" value="value1">
    <input type="checkbox" name="chkSingle" id ="chkSingle2" value="value2">
    <input type="checkbox" name="chkSingle" id ="chkSingle3" value="value3">
    <button onclick="alert(getCheckedItems('chkSingle'))">Click</button>
    <script type="text/javascript">
        function getCheckedItems(checkboxName) {
            var checkboxes = document.querySelectorAll('input[name="'+checkboxName+'"]:checked'), values = [];
            Array.prototype.forEach.call(checkboxes, function(el) {
            values.push(el.value);
            });
            return values;
        }
    </script>
</body>
</html>

编辑2:

也试试这个方法。

function getCheckedItems(checkboxName) {
        var checkboxes = document.getElementsByName(checkboxName);
                var selected = [];
                for (var i=0; i<checkboxes.length; i++) 
                {
                    if (checkboxes[i].checked) 
                    {
                         selected.push(checkboxes[i].value);
                }
         }
       return selected;
    }

此编辑与问题无关。此编辑是应提问者的要求

当您动态添加复选框时,请执行以下操作:

//this is your function I assume.
function toCreateCheckBoxesDynamically()
{
    create checkboxes dynamically;
    attach an attribute as onclick="function WhichTakesCheckedValuesAndAttachValuesToHiddenFields('dynamicallyCreatedCheckBoxName')" to the button.

                                        OR

    //bind a click event to the button and call this function "function WhichTakesCheckedValuesAndAttachValuesToHiddenFields('dynamicallyCreatedCheckBoxName')"
    document.getElementById("yourButton").addEventListener("click", function()
    {
          whichTakesCheckedValuesAndAttachValuesToHiddenFields('dynamicallyCreatedCheckBoxName');
    });
}

// this is the function which you call on button click while submitting the form
function whichTakesCheckedValuesAndAttachValuesToHiddenFields(checkboxName)
{
     var checkboxes = document.getElementsByName(checkboxName);
            var selected = "";
            for (var i=0; i<checkboxes.length; i++) 
            {
                if (checkboxes[i].checked) 
                {
                      if(selected == "" || selected == 'undefined')
                             selected = checkboxes[i].value;
                      else
                             selected = selected +","+ checkboxes[i].value;
                }
        }

    document.getElementById("hiddenFieldName").value = selected ;// the stored value is comma separated string so retrieve it on the server by using split method which will give you string array and then do your further work.
    document.getElementById("yourFormName").submit(); // submit this form
}

隐藏字段语法:

<input type=hidden name="yourHiddenFieldName" value="">// inside your form in html

或者还有一个提示

您还可以将动态创建的复选框名称保存在表单的隐藏字段中,并在提交后根据该隐藏字段值获取这些选中的值

String checkBoxName = request.getParameter('hiddenFieldName'); 

然后获取所有checkBoxes值

String checkedboxes[] = request.getParameterValues(checkBoxName); 

执行此操作,您将获得服务器端所有复选框的值。

关于java - 需要一个在jsp中读取动态复选框ID的javascript函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30771790/

相关文章:

java - 如何使用 Java SDK 使 Cloudfront 中的缓存失效

javascript - 如何确保点击事件只对预期的类产生影响?

javascript - 今天全日历开始日期

jsp - tomcat 7 - org.apache.jasper.JasperException 包含无效表达式 : javax. el.ELException:无法解析表达式

java - Android ZXING条码可以将结果值放入字符串

java - 如何存储巨大的瓦片 map ?

java - 插入通用 Number ArrayList Java

javascript - ContentFlow : How to tap and scroll, 和点击时翻转图像

java - 输入文本验证错误 - JSP

css - eclipse 上的 JSP : applying inline css in a conditional statement