2

我想编写一个java脚本函数,它将从td获取类型为“复选框”的输入标签并将它们标记为未选中。下面是jsp代码:

<td id="uncheckedByDefault" align=center class=listTypeOne>
    <% if (priv.getViewPrivilege() == 1) { %>
    <input name="view" type="checkbox" value='<%= priv.getAttributeId() %>' checked>                     
    <% } else { %>
    <input name="view" type="checkbox" value='<%= priv.getAttributeId()%>'>
    <% } %>
</td>

现在这里的 td 有 id="uncheckedByDefault"。我想获取所有输入标签并将它们设置为未选中。谁能告诉我该怎么做。提前致谢。

4

5 回答 5

2

尝试:

inputs = document.getElementById('uncheckedByDefault').getElementsByTagName('input');

    for (index = 0; index < inputs.length; ++index) {
        // deal with inputs[index] element.
       inputs[index].checked=false;
    }
于 2013-05-21T07:49:16.690 回答
1

我不知道我是否理解你的问题,但我会试一试。

首先,您可以使用 a 将元素打印到您的网页中<% ... %>(我将在我的回答中展示如何),因此您不必将条件设置为<% ... %>、打印元素......等等。所有这些都在同一个 Java 函数中.

其次,您可以在不调用 getter 的情况下访问您的Object属性。如果您想将他的值设置为特定字段,则根本不需要。(IE)<input type="text" name="clientName" value="${client.clientName}" />

这是我对您的问题的建议:

<td id="uncheckedByDefault" align=center class=listTypeOne>
    <%
        if (priv.getViewPrivilege() == 1) {
            out.println("<input name='view' type='checkbox' + 
            value=" + priv.getAttributeId() + " checked = 'checked'>");
        } else {
            out.println("<input name='view' type='checkbox' + 
            value=" + priv.getAttributeId() + ">");
        }
    %>
</td>

报告它是否有效或不是您想要的。

更新

如果您希望它们都未选中,因为您的元素id属性建议这样做。为什么要检查对象属性以进行设置checked?只需添加您的元素,不要将其设置为选中的属性。

<input name='view' type='checkbox' value="${priv.attributeId}">

于 2013-05-21T07:32:58.197 回答
1

这是您需要的函数,您可以随时调用它:

function uncheckAll(){
    var inputsContainer = document.getElementById('uncheckedByDefault');
    var allInputs = inputsContainer.getElementsByTagName('input');
    var len = allInputs.length;
    for(var i=0; i<len; i++){
         if(allInputs[i].type == 'checkbox'){
         allInputs[i].checked = false;
         }
    }
}
于 2013-05-21T07:40:01.277 回答
0

You just need to get a list of all of the elements you're interested in, then you need to perform some action on them. I've added the forEachNode function, since the querySelectorAll function doesn't actually return an array - it returns a nodeList which has similar syntax and functionality as an array, but omits the forEach function.

Note: a checkbox is checked if it has the attribute checked. The actual value of the attribute is not used - it's just it's presence or lack thereof that dictates the check-state. As such, you could write anything rather than 0 to the checked attribute.

EDIT: The above note is incorrect. Both the 'checked' attribute as visible in the html and the 'checked' member variable control the state. I've updated the uncheck and check functions.

<!DOCTYPE html>
<html>
<head>
<script>
function byId(e){return document.getElementById(e);}

window.addEventListener('load', mInit, false);

function mInit()
{
}

/*
     func to be called takes 3 variables. currentElement, currentIndex, nodeList the element belongs to.
*/
function forEachNode(nodeList, func)
{
    var i, n = nodeList.length;
    for (i=0; i<n; i++)
    {
        func(nodeList[i], i, nodeList);
    }
}
/*
function uncheck(elem)
{
    elem.removeAttribute('checked');
}

function check(elem)
{
    elem.setAttribute('checked',0);
}
*/

function uncheck(elem)
{
    elem.checked = false;
    elem.removeAttribute('checked');
}

function check(elem)
{
    elem.checked = true;
    elem.setAttribute('checked',0);
}



function checkAll()
{
    var checkBoxElements = document.querySelectorAll('input[type=checkbox]');
    forEachNode(checkBoxElements, check);
}

function uncheckAll()
{
    var checkBoxElements = document.querySelectorAll('input[type=checkbox]');
    forEachNode(checkBoxElements, uncheck);
}

</script>
<style>
</style>
</head>
<body>
    <button onclick='uncheckAll()'>Uncheck all</button>
    <button onclick='checkAll()'>Check all</button>
    <br>
    <input type='checkbox'/>CB 1
    <br>
    <input type='checkbox'/>CB 2
    <br>
    <input type='checkbox'/>CB 3
    <br>
    <input type='checkbox'/>CB 4
    <br>
    <input type='checkbox'/>CB 5
    <br>
</body>
</html>
于 2013-05-21T07:37:36.740 回答
0

这应该工作

var allInputs = document.getElementById("uncheckedByDefault").getElementsByTagName("input");
for (i = 0; i < allInputs.length; i++) {
    if (allInputs[i].getAttribute("type") == 'checkbox') {
        allInputs[i].removeAttribute("checked");
    }
}
于 2013-05-21T07:38:56.920 回答