当我选择单选按钮时如何禁用多个复选框?

时间:2013-02-12 13:18:44

标签: javascript

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page isELIgnored="false" %>
<%@ page import="java.util.*"%>

<%@page import="com.ba.RetrieveDetails"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
history.forward();
function validate(logFile){

    var count=0;

    for (var i=0; i<logFile.length; i++) {
        if (logFile[i].checked) {

            count++;
        }

    }

    if (count < 1) 
    {
    alert("please select Log Fille(s)");
    return false;
    }

    }

</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Log Files</title>


</head>
<body>
<form name="LogFiles" action="RetrieveLogFiles" method="get" onsubmit="">
<%
HashMap hs=new HashMap();
hs=(HashMap)session.getAttribute("key");

session.getAttribute("key");

String hosts[]=(String[])session.getAttribute("hosts");
%>
<br>
<br>
<img src="BA_Logo1.bmp" align="right" alt="British Airways">
<table>
<tr>
<%
if(hs.size()== 0)
{%>
    <td><h3>Authentication fail OR Does not have access to UnixBox.Click <a href="Login.jsp">Here</a> to Login</h3></td>
<%
}
else
{
for(int i=0;i<hosts.length;i++)
{
    %>

    <h3><p><%out.println(hosts[i]+"\n");%></p></h3>

    <%


    ArrayList al=new ArrayList();
    al=(ArrayList)hs.get(hosts[i]);
    int flag=al.size();


    %>
    <td>
    <table border="2" cellpadding="4">  

    <%
    if(flag==0)
    {%>

    <td>
    <h3><p style="color:#FF0000">No Log Files to display</p></h3>
    </td>


    <%

    }else{
    for(int j=0;j<al.size();j++)
    {



        String lines[]=al.get(j).toString().split("\\r?\\n");



        for(int len=0;len<lines.length;len++)
        {

            %>

        <tr>
        <td><%out.println(lines[len]);%></td>

    <td><input type="checkbox" name="logFile" id="logFile" value="<%=hosts[i]+"$"+lines[len]%>"></td>
    <td><input type="radio" name="logFileView" id="logFileView" value="<%=hosts[i]+"$"+lines[len]%>"></td>
    </tr>


        <%
        }%>

        </table>
        </td>
        </tr>
        </table>
<%  }

}   

    }%>
<input type="submit" name="Get Logs" align="middle" value="Get Logs" onclick="return validate(logFile)" >
<%
}

%>


</form>
</body>
</html>

这里当我按下logFileView中的任何单选按钮时,必须禁用所有logFile复选框?反之亦然。请帮忙解决这个问题

logFileView是单选按钮,logFile是复选框。这里有多个复选框和多个单选按钮。

1 个答案:

答案 0 :(得分:0)

您可以使用这些JavaScript code来实现 首先,它循环以获取所有元素(例如:复选框),然后向它们添加onclick事件侦听器。然后内部循环是在单击复选框时禁用所有单选按钮,相同的逻辑应用于单选按钮:

var logFiles = document.getElementsByName("logFile");
var logFileViews = document.getElementsByName("logFileView");

//To disable all radio buttons when click on any check box
for (var i=0, count=logFiles.length; i < count; i++) {
    logFiles[i].onclick = function() {
        for (var j=0, count=logFileViews.length; j < count; j++) {
            logFileViews[j].disabled = true;
        }
    };
}

//To disable all check boxes when click on any radio button
for (var i=0, count=logFileViews.length; i < count; i++) {
    logFileViews[i].onclick = function() {
        logFiles.disabled = true;
        for (var j=0, count=logFiles.length; j < count; j++) {
            logFiles[j].disabled = true;
        }
    };
}