Zaw Myo Htet
Zaw Myo Htet

Reputation: 540

Get value from radio button and add in url

I have a list with radio button.When user checked the radio,it take the value of radio button and add it in the url.How can I complete this function? Following is my simple code. But it doesn't work.Help me,plz.

<script type="text/javascript">
$(document).ready(function($){
    var val = $(':radio[name=rdo_1]:checked').val();
    $('#button').click(function(){
        document.location.href = 'http://localhost/test/val1.html';

    });
});
</script>

Upvotes: 1

Views: 3074

Answers (6)

Ankush Jain
Ankush Jain

Reputation: 1527

try the following

<script type="text/javascript">
$(document).ready(function($){

    $('#button').click(function(){
        var val = $('input[type=radio][name=rdo_1]:checked').val();
        var url="http://localhost/test/"+val+".html";
        window.location.href = url;

    });
});
</script>

Upvotes: 0

Cihad Turhan
Cihad Turhan

Reputation: 2849

Use <input type="submit"/> enclosed with <form method="get"> </form>

Example would be like below.

But don't forget to add name fields

<form action="http://localhost/test/val1.html?" method="get" name="f">
    <input type="text" name="user" />
    <input type="checkbox" name="name1" />
    <input type="submit" class="login_button" value="LOGIN">
</form>

It's better since you don't use javascript

Upvotes: 0

Allan Kimmer Jensen
Allan Kimmer Jensen

Reputation: 4389

You want the var assignment in the button click handler. If you don't do this, it will be assigned upon load, and any changes will not affect the function.

$(function() {
    $('#button').click(function(){
        var val = $(':radio[name=rdo_1]:checked').val();
        document.location.href = 'http://localhost/test/val1.html?radio_button_value=' + val
    });
});

Note: I also used the shorthand version of document ready.

Upvotes: 0

user1438327
user1438327

Reputation:

jQuery:

$('input[@name="NAME"]:checked').val();

javascript:

var radios = document.getElementsByName('NAME');

for (var i = 0, length = radios.length; i < length; i++) {
if (radios[i].checked) {
    alert(radios[i].value);
}
}

Upvotes: 0

Devang Rathod
Devang Rathod

Reputation: 6736

$(document).ready(function($){
      $('#button').click(function(){
          var val = $(':radio[name=rdo_1]:checked').val(); 
          document.location.href = 'http://localhost/test/val1.html?radio='+val; 
      });
});

Upvotes: 0

vinothini
vinothini

Reputation: 2604

You can send params like this

<script type="text/javascript">
$(document).ready(function($){
    var val = $(':radio[name=rdo_1]:checked').val();
    $('#button').click(function(){
        document.location.href = 'http://localhost/test/val1.html?radio_button_value='+val

    });
});
</script>

Upvotes: 1

Related Questions