通过检查单选按钮选择Ajax调用

Choosing Ajax Calls by checking radio button

本文关键字:Ajax 调用 选择 单选按钮 检查      更新时间:2023-09-26

这是我的示例代码。在这里,单击单选按钮时,应该检查if条件并触发下面的ajax调用。但它总是触发第一个ajax调用

$(document).ready(function (){
$('#save').click(function (event)
{
    if ($("#im").is(":checked")) //check not working
    {
        $.get('../bus.php',
        {
            fxn: 'A',
            jsn: '{"name":"' + name + '"}'
        });
    }
    else if ($("#gm").is(":checked"))
    {
        $.get('../bus.php',
        {
            fxn: 'B',
            jsn: '{"name":"' + name + '"}'
        });
    }
    else
    {
        $.get('../bus.php',
        {
            fxn: 'C',
            jsn: '{"name":"' + name + '"}'
        });
    }
});});
<html>
<body>
    <input type=radio name="radio1" value="1" id="im"><label>A</label> 
    <input type=radio name="radio1" value="2" id="gm"><label>B</label>
    <input type=radio name="radio1" value="3" id="am"><label>c</label>
</body>

代码对我来说也是完美的。

$(document).ready(function () {
$('#save').click(function (event) {
    if ($("#im").is(":checked")) //check not working
    {
        $.get("http://api.openweathermap.org/data/2.5/weather?q=London",function( data ){
            console.log(data);
        });
    } else if ($("#gm").is(":checked")) {
        $.get("http://api.openweathermap.org/data/2.5/weather?q=LosAngeles",function( data ){
           console.log(data);
        });
    } else {
        $.get("http://api.openweathermap.org/data/2.5/weather?q=NewYork",function( data ){
            console.log(data);
        });
    }
});

});http://jsfiddle.net/2P72N/

您确定问题不在"fxn"参数管理的php代码中吗?

怎么样?

var val = $("input[name='radio1']:checked").val();
switch (val) {
case : "1":
// code
break;
case : "2":
// code
break;
case : "3":
// code
break;
}

Try

if ($('#im').prop('checked'))
{
  //Your Code
}

尝试在选择器中筛选:checked

$(document).ready(function () {
    $('#save').click(function (event) {
        if ($("#im:checked").length) {
            console.log('IM');
        } else if ($("#gm:checked").length) {
            console.log('GM');
        } else {
            console.log('AM');
        }
    });
});

更新小提琴