简体   繁体   中英

How to get checked value of Kartik radioList in javascript

I would like to know how to get checked value in javascript. Here is my _form.php

        <?= $form->field($model, 'rdo')->radioList(["0"=>"Free","1"=>"Paid"], ['id' => 'rdo'])->label('Select Contribution') ?>
        <input type="text" id="name" name="name">

This is javascript code.

    $(document).ready(function(){
            $("#rdo").click(function(){
            $(":text").hide();
        });
            $("#rdo1").click(function(){
            $(":text").show();});
    });

I would like to appear text box when user choose free radio button and also text box will disappear when user choose paid radio. Thank.

You can access the value of current checked option by doing this:

$('#rdo input:checked').val();

// Or using name property
$('input[name="YourModelName[rdo]"]:checked').val();

Undefined is returned if nothing is selected.

So you should be able to do something like this:

$('input[name="YourModelName[rdo]"]').change(function () {
    if($('input[name="YourModelName[rdo]"]:checked').val() == 0) {
        // Do something
    }
    else if(.....){
        // Do something else
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM