I found this answer before, to fire an alert if the button is pressed but the checkbox isn't checked.
Why won't this work?
I have this in the head
<script src="http://code.jquery.com/jquery-latest.js"></script>
And this in the body:
<input value="1" type="checkbox" name="salgsvilkar" ID="checkbox2" style="float:left;"
onclick="document.getElementById('scrollwrap').style.cssText='border-color:#85c222; background-color:#E5F7C7;';" /><label for="checkbox2" class="akslabel">Salgs og leveringsvilkår er lest og akseptert</label>
</span>
{literal}
<script type="text/javascript">
$(function() {
//checkbox
$("#checkbox2").click(function(){
//if this...
//alert("this")...
if($("#checkbox2").is(':checked'))
{
alert("im checked");
}
});
//button
$("#fullfor_btn").click(function(e){
if(!$("#checkbox2").is(':checked'))
{
alert("you did not check the agree to terms...");
e.preventDefault();
}
});
}
</script>
{/literal}
This on another .tpl:
<label></label>
<button type="submit" class="submi开发者_高级运维t" name="{$method}" id="fullfor_btn" title="Fullfør bestillingen nå" value=""> </button>
What could be going wrong? The jQuery doesn't fire anything at all.
update the id terms was changed to checkbox2, still nothing happening.
Your checkbox has the id "checkbox2", but you are trying to access it using the id "terms" in the click handler for the button.
update
You are missing the closing parenthesis for the $(function(){...})
call. This throws a syntax error when the page is loaded, and the code in the script block is never run. Change the last line in the script block from:
}
to:
});
I have tested the code with this change, and it works fine.
Try this:
$(document).ready(function() {
$("#checkbox2").click(function() {
if ($(this).attr("checked")) {
alert("im checked");
}
});
$("#fullfor_btn").click(function(e) {
if ($("#checkbox2").attr("checked") == false) {
alert("you did not check the agree to terms...");
e.preventDefault();
}
});
});
to fire an alert if the button is pressed but the checkbox isn't checked.
Try this if you press but the check box is not checked as you said above:
$("#checkbox2").click(function(){
if($("#checkbox2").is(':checked') == false)
{
alert("im checked");
}
});
.
Updated based on comment:
To show an alert if checked, you can simple do like:
$("#checkbox2").click(function(){
if($("#checkbox2").is(':checked'))
{
alert("im checked");
}
});
Also from your code, it seems that you are missing the );
at the end for the:
$(function()
There isn't any element with id="terms"
in your HTML.
精彩评论