简体   繁体   中英

Send value of selected radio button to ajax function

<select onChange="getclass(this.value); name="Course_ID" >

I have an ajax function 'getclass' which gets called whenever i change the value of my dropdown list. I want to pass the value of a radio button as well to the ajax function whenever this function gets called.

<input type="radio" name="type" value="c" checked="checked"> Class<br>

Here is the ajax function

function getclass(val1) {
    $.ajax({
        type: "POST",
        url: "get_class.php",
        data:'id='+val1,
        success: function(data){
        $("#class-list").html(data);
        }
    });
}

You can pass radio button value to a variable and use in ajax:

function getclass(val1) {
    var radVal = $("input[type=radio][name=type]:checked").val();
    $.ajax({
        type: "POST",
        url: "get_class.php",
        data: { 'id': val1, 'radVal': radVal },
        dataType: 'JSON',
        success: function (data) {
            $("#class-list").html(data);
        }
    });
}

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