0

Full page is at http://f14.co/auto-search/reno

I have the following checkbox set up outside a form:

<div class='span5' style='margin-left:0px !important;'>
    <label for='model0'>
    <input type="checkbox" name="model0x" id="model0x"
    value="Accord" style='margin-top:-5px !important;'> Accord</label>
</div>      

I have this javascript between the checkbox and the form:

<script>
    if($("#model0x").is(':checked')){
      $("#model0_is_checked").val($("#model0x").val());
    }else{ 
      $("#model0_is_checked").val("Not Checked"); 
    }   
</script>

Finally, I have this hidden input to call that value inside the form when the item is checked or not:

 <form method="post" class="form-horizontal" id="final_form" action="send_mail.php">
 <input type="hidden" id="model0_is_checked" name="model0_is_checked">
   MORE FORM STUFF AND SUBMIT BUTTON 
 </form>

No matter what I'm getting no value in the send_mail.php ....what am I doing wrong?

4
  • how are you calling that if()? On page load it looks like it will return false, and unless you re-call it on form submit, or other event, it will not be re-called. Commented Aug 8, 2013 at 1:29
  • You can see the code at f14.co/auto-search/reno Commented Aug 8, 2013 at 1:32
  • You dont want inputs outside the <form> its bad form. Commented Aug 8, 2013 at 1:38
  • Also model0_is_checked doesnt exist yet. Your trying to set an object the browser hasnt rendered yet. Try just moving your javascript to the bottom of the page so it runs after the page has rendered. Commented Aug 8, 2013 at 1:43

3 Answers 3

1
$('#model0x').click(function(){
    var self=this;
    $("#model0_is_checked").val($(self).is(':checked')?self.value:'Not Checked');
    // do something
});
// or use submit ()
Sign up to request clarification or add additional context in comments.

Comments

0

Bind the function to the form submit.

<script>
$('#final_form').on('submit',function(){
    if($("#model0x").is(':checked')){
        $("#model0_is_checked").val($("#model0x").val()); }
    else { $("#model0_is_checked").val("Not Checked");}
});
</script>

jsFiddle example http://jsfiddle.net/KZrLp/

1 Comment

Do I need to change my hidden input at all within the form?
0

Your Javascript runs once, when the page (or more accurately, the <script> tag) is loaded. You have to make it run when the form is submitted instead:

<script type="text/javascript">
function updateHidden() { // Find a better name ;)
    if($("#model0x").is(':checked'))
        $("#model0_is_checked").val($("#model0x").val());
    else
        $("#model0_is_checked").val("Not Checked");
}
$(document).ready(function() { $('#final_form').submit(updateHidden); });
</script>

P.S. : not tested code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.