WebDevDanno
WebDevDanno

Reputation: 1122

Change radio button 'checked' attribute on form submit

I've got two radio buttons. Which a user can select and submit dynamically to change the content of a HTML table I have.

How can I use jQuery to set the 'checked' attribute to the appropriate radio button. So, United Kingdom is checked by default and thus filled in 'blue'. When a user clicks Wales the radio button that is checked will be Wales and not United Kingdom.

This also needs to be reversible (i.e. selected UK after selecting Wales).

Here is my code:

<input type="radio" name="region" value="0" checked="checked" /> United Kingdom 
<input type="radio" name="region" value="1" /> Wales

And my current Javascript which just submits the form:

$('input[name=region]').change(function(){
                    $('form').submit();
                });

Please note that the form submits to the same webpage as that's where my HTML <table> is located.

Upvotes: 0

Views: 862

Answers (2)

Nikesh Tripathi
Nikesh Tripathi

Reputation: 17

see this code i hope it works.

jQuery(document).ready(function($) {
    $('input[name=region]').change(function(){
        alert(this.value);
                $('form').submit();
            });
});

Upvotes: 1

Jacek Pietal
Jacek Pietal

Reputation: 2019

if your form is method="POST" you need php:

<?php if(isset($_POST['region']) && $_POST['region'] == 1)) { echo 'checked="true"'; } ?>

or if its method="GET"

you can and set it with jQuery using getting parameter from url (example: http://www.jquerybyexample.net/2012/06/get-url-parameters-using-jquery.html )

if

var param = 'your value of region param' 

then do

$(function() {
    $('input[name="region"][value="' + param + '"]')[0].checked = true; 
});

Upvotes: 0

Related Questions