...

View Full Version : Checkbox help



SteveH
06-19-2008, 01:25 PM
Hello

I am trying to produce a disclaimer ('Yes, I agree', 'No, I don't') using checkboxes in JavaScript.

If neither box is checked and the user presses 'Submit', they ought to get an alert box informing of the user of this. The folliwng is supposed to do this, but no alert box shows. I have tried looking at the script through IE7 and Firefox 3.

If possible, I would also like the page to immediately redirect to my main page when the 'I agree' check box is selected (but not when the 'I do not agree' checkbox is selected). This removes the need for the 'Submit' button. At the same time, the 'I do not agree' checkbox should be defaulted.

This is what I have so far:

<SCRIPT TYPE="text/javascript" LANGUAGE=JAVASCRIPT>

function checkCheckBoxes() {
if (document.frmTest.CHKBOX_1.checked == false &&
document.frmTest.CHKBOX_2.checked == false)
{
alert ('Please select a checkbox');
return false;
}
else
{
return true;
}
}

</SCRIPT>


And then this:

<form onsubmit="return checkCheckBoxes();" action="">
<input type="checkbox" name="CHKBOX_1" value="1">I do not agree to the terms</p>
<input type="checkbox" name="CHKBOX_2" value="2">I agree to the terms</p>
<input type="submit" value="Submit!" />
</form>

Thanks for any help with my awkward questions!

Steve

Philip M
06-19-2008, 01:37 PM
As it stands you problem, is:-

<form name = "frmTest" onsubmit="return checkCheckBoxes();" action=""

But if you want to redirect when "I agree" is clicked:-

<script type="text/javascript">
function redirect() {
window.location = mainpage.html; // of course you must specify the correct name of your page
}
</script>

<form name = "myform">
<input type="checkbox" name="CHKBOX_1" value="1" checked>I do not agree to the terms</p>
<input type="checkbox" name="CHKBOX_2" value="2" onclick = "redirect()">I agree to the terms</p>
</form>


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

SteveH
06-19-2008, 02:10 PM
Hello Phillip

Many thanks for your reply. You make it look so easy!

The page loaded with 'I do not agree' already checked as I wish, but it didn't redirect - a little yellow exclamation mark appeared in the bottom left-hand corner of the screen.

So I replaced myfile.html with 'myfile.html' and it is OK now.

Very many thanks.

Steve



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum