<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<html lang="en">
<head>
<title>checkbox disabling</title>
<script type="text/javascript">
function disableHandler (form, inputName) {
var inputs = form.elements[inputName];
for (var i = 0; i < inputs.length; i++) {
var input = inputs[i];
input.onclick = function (evt) {
if (this.checked) {
disableInputs(this, inputs);
}
else {
enableInputs(this, inputs);
}
return true;
};
}
}
function disableInputs (input, inputs) {
for (var i = 0; i < inputs.length; i++) {
var currentInput = inputs[i];
if (currentInput = input) {
currentInput.disabled = true;
}
}
}
</script>
</head>
<body>
<form name="aForm" action="">
<p>
<label>
Blue
<input type="checkbox" name=details[] value="blue">
</label>
<label>
Green
<input type="checkbox" name=details[] value="green">
</label>
<label>
Red
<input type="checkbox" name=details[] value="red">
</label>
</p>
</form>
<script type="text/javascript">
disableHandler(document.forms.aForm, 'details[]');
</script>
</body>
</html>
Tha above code is running…but i want some modification in this code…such that…
when i select some checkboxes and submit this form…and when we again goes to this page via request/responce from web server, it should display previoue checked checkboxs as disabled.and remaining checkboxes as enabled.
I have stuck this problem from last 2 days…so please, help me out.
do you need the previously checked checkboxes to be disabled if the page is revisited in just the same session or in new sessions after the browser has been closed and re-opened as well?
if it’s just for one session then you can use session variables to store which checkboxes were checked, otherwise you will have to store them in either cookies, a file of some sort or a database. Each have their pros and cons.
eitherway, I wouldn’t use javascript because it then won’t work in browsers where javascript is disabled for any reason.
Thanks both of you for your valuable reply…
I want to store checked checkboxes for that session only.
and i have written code in javascript, its not compulsory, so, you can give example of jsp also.
please, help me.
Thank you for your help in advance:)
<html lang="en">
<head>
<title>checkbox disabling</title>
<script type="text/javascript">
function disableHandler(form, inputName) {
var inputs = form.elements[inputName];
for (var i = 0; i < inputs.length; i++) {
var input = inputs[i];
input.onclick = function (evt) {
if (this.checked) {
disableInputs(this, inputs);
}
else {
enableInputs(this, inputs);
}
return true;
};
}
}
function disableInputs(input, inputs) {
for (var i = 0; i < inputs.length; i++) {
var currentInput = inputs[i];
if (currentInput = input) {
currentInput.disabled = true;
}
}
}
function BeforeSubmit(f) {
var checkList = document.getElementById("checkboxList").getElementsByTagName("input");
var vals = "";
for (var i = 0; i < checkList.length; i++) {
if (checkList[i].type == "checkbox" && checkList[i].checked) {
vals += checkList[i].value + ",";
};
}
if (vals.length > 0) {
f.action += "selected=" + vals.substring(0, vals.length - 1);
}
return true;
}
Request = {
QueryString: function (item) {
var svalue = location.search.match(new
RegExp('[\\?\\&]' + item + '=([^\\&]*)(\\&?)', 'i'));
return svalue ? svalue[1] : svalue;
},
All: function () {
var cond = {};
var urlArr = location.search.substring(1, location.search.length).split("&");
for (var i = 0; i < urlArr.length; i++) {
var param = urlArr[i].split("=");
if (param.length > 1) {
if (cond[param[0]]) {
if (!(cond[param[0]] instanceof Array)) {
cond[param[0]] = [cond[param[0]]];
}
cond[param[0]].push(unescape(param[1]));
}
else
cond[param[0]] = unescape(param[1]);
}
}
return cond;
}
}
window.onload = function () {
var selected = Request.QueryString("selected");
if (selected != null && selected != "") {
var checkList = document.getElementById("checkboxList").getElementsByTagName("input");
for (var i = 0; i < checkList.length; i++) {
if (checkList[i].type == "checkbox" && checkList[i].name == 'details[]' && selected.indexOf(checkList[i].value) > -1) {
checkList[i].checked = true;
checkList[i].disabled = true;
};
}
}
}
</script>
</head>
<body>
<form name="aForm" id="aForm" action="?" method="post" onsubmit="return BeforeSubmit(this)">
<p id="checkboxList">
<label>
Blue
<input type="checkbox" name="details[]" value="blue">
</label>
<label>
Green
<input type="checkbox" name="details[]" value="green">
</label>
<label>
Red
<input type="checkbox" name="details[]" value="red">
</label>
</p>
<input type="submit" value="submit" />
</form>
<script type="text/javascript">
disableHandler(document.forms.aForm, 'details[]');
</script>
</body>
</html>
Hey, this only used JavaScript . if the page is not from itself,give the url param like ‘selected=blue,red’.
the param value is the value of the checkboxes and split by ‘,’