在选择单选按钮上做一些事情 [英] do something on selecting a radio button

查看:91
本文介绍了在选择单选按钮上做一些事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在选择单选按钮时借助jquery执行某些操作.

I want to perform something with the help of jquery when a radio button is selected.

假设页面上有n个单选按钮组

Suppose the page has n number of radio button groups

<input type="radio" name="group1" value="1" onSelect="jquery ajax" /><br>
<input type="radio" name="group1" value="2" onSelect="jquery ajax" /><br>


<input type="radio" name="group2" value="1" onSelect="jquery ajax"/><br>
<input type="radio" name="group2" value="2" onSelect="jquery ajax" /><br>

以此类推...

每个onSelect调用相同的js函数(假设为jsForAjaxCall(args)),该函数启动jquery ajax调用.

Each onSelect calls the same js function(suppose, jsForAjaxCall(args)) which initiates the jquery ajax call.

无论何时选择或取消选择单选按钮,我都希望有一个jquery ajax调用.我把它展示出来供选择.

When ever a radio button is selected or deselected I want have a jquery ajax call. I showed it for selection.

1)以这种方式调用JavaScript来取消选择单选按钮的方式是什么?取消选择吗?如果是,它将与所有浏览器兼容吗?我没有找到答案.

1) what is the way to have such a javascript call for unselecting a radio button? is it onUnselect? if yes, will it be compatible with all browsers? i did not find the answer.

2)假设用户一个接一个地选择单选按钮(一组中的一个按钮),并进行了相应的ajax调用.

2)Suppose user selects one after another radio buttons (one button from a group) and the corresponding ajax calls are made.

但是我希望ajax调用一个接一个地完成. Ajax呼叫1完成,呼叫2开始,呼叫2完成,呼叫3开始,依此类推.

But I want the ajax calls to be completed one after another. Ajax call 1 completes, call2 begins, call2 completes , call3 begins and so on.

但是由于异步特性,在我描述的情况下可能不会发生这种情况 阿贾克斯

But this might not happen in the situation i described because of the asynchronous nature of ajax.

因此,我需要让后续的调用保留在前面的jquery ajax调用的回调函数中.我找不到办法

So I would need to have the succeeding calls stay in the callback function of the preceding jquery ajax calls. I found no way to do it

我如何实现我的目标?

我的脚本语言是php

my scripting language is php

假定用户一个接一个地单击n个单选按钮(一组中的一个按钮).对于每个按钮选择,都会进行一次同步ajax调用,以使二维会话数组保存所选单选按钮的值,以便在页面刷新后,这些值可用于保留对单选按钮的选择.在这种情况下,应更改最初编写的html单选按钮代码.

suppose the user clicks n radio buttons one after another (one button from a group). For each button selection, a synchronous ajax call is made to make a 2-dimensional session array hold the values of the selected radio buttons so that after the page refresh , those values can be used to retain the selection of the radio buttons. The html radio button code written in the first place should be changed in that case.

我的问题是:

用户完成单击n个按钮(即n = 10),然后刷新页面.进行了5次同步ajax调用,其余r尚未执行.然后,在页面刷新后,是否将保留最后5个单选按钮选择的值?如果不是,该如何保留那些人.

The user finishes clicking n buttons(i.e n=10) and then s/he refreshes the page. 5 synchronous ajax calls are made and the remaining r yet to be made. Then after page refresh, will the values of the last 5 radio button selections be retained? If not how to retain those in the said situation.

推荐答案

基于nyfer的答案,您可以将$ .ajax的成功回调中的ajax调用链接起来:

Building on nyfer's answer, you could chain the ajax-calls in the success-callback of $.ajax:

$("input[name*='group1']").click(function(){
    $.ajax({

    success: function(){
        $.ajax({
            success:function(){
                $.ajax({});
            }
        });
    }
    });
});

这是一种凌乱的代码,因此我可能会将成功回调放在命名函数中,而不是上面显示的匿名函数.现在,如果您希望按问题提示中的每个按钮单击一个Ajax调用,我将实现一个队列来排队和使Ajax调用出队.将项目放入click事件中,然后在每个ajax调用的成功回调中重新访问队列,以查看是否还有要处理的项目.

It's sort of messy code, so I'd probably put the success-callbacks in named functions as opposed to anonymous functions as displayed above. Now, if you want one ajax call per button clicked as hinted in the question, I would implement a queue to enqueue and dequeue ajax calls. Enqueue items in the click event, and revisit the queue in the success-callback of every ajax-call to see if there are items left to process.

这篇关于在选择单选按钮上做一些事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆