通过检查单选按钮选择Ajax调用
Choosing Ajax Calls by checking radio button
这是我的示例代码。在这里,单击单选按钮时,应该检查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');
}
});
});
更新小提琴
相关文章:
- 另一个ajax调用中的Jquery ajax调用在for循环中没有按预期工作
- Javascript/jQuery中的并行Ajax调用
- 任何方式使AJAX调用Gmail API,而无需通过JS库
- node.js请求数据事件未在CORS ajax调用中触发
- 如何从ajax调用返回.wrap()元素
- Ajax调用在Firefox中不会自动响应
- AJAX调用中的非法调用
- 使用AJAX调用将GeoJSON数据拉入传单
- 在MVC 4中,对Controller的Ajax调用为空
- Jquery:对返回JSON数据的php脚本的Ajax调用
- 在ajax调用中阻止来自JS对象的函数
- 如何在MVC5中ajax调用的部分视图中引用外部javascript
- 页面加载后的Ajax调用
- 如何使用Ajax调用特定的控制器和操作来重定向用户
- curl和fileget_contents在ajax调用后不起作用
- 在任何AJAX调用之前触发一个javascript函数
- 无法在mvc视图中使用ajax调用获取操作返回的模型对象列表
- Ajax调用ERror Cross Origin REquest:在rails中自动完成大学列表
- ASP.NET MVC 3-在ajax调用后,重定向到新页面或生成页面刷新
- 点击功能没有'ajax调用动态元素(Backbone)后无法工作