简体   繁体   English

根据按钮ID检测单选按钮更改

[英]Detect radio button changes based on a button id

In some circumstances I have to detect radio-buttons changes. 在某些情况下,我必须检测单选按钮的更改。 The problem is the app only have the ID of the button it is interested. 问题是该应用仅具有其感兴趣的按钮的ID。 For example, given that HTML piece: 例如,鉴于HTML片段:

<label class="required">Type</label><br/>
<span class="vertical">
    <input checked="checked" id="diabetes_type-none" name="diabetes_type" type="radio" value="none" /><label for="diabetes_type-none">No diabetes</label><br />
    <input id="diabetes_type-dm1" name="diabetes_type" type="radio" value="dm1" /><label for="diabetes_type-dm1">DM1</label><br />
    <input id="diabetes_type-dm2" name="diabetes_type" type="radio" value="dm2" /><label for="diabetes_type-dm2">DM2</label><br />
    <input id="diabetes_type-other" name="diabetes_type" type="radio" value="other" /><label for="diabetes_type-other">Other type</label>
    <input class="small free" id="diabetes_type-other_more" name="diabetes_type-other_more" type="text" />
</span>

and given the ID "diabetes_type-other" the app must execute some code each time user checks or unchecks the "Other type" button. 并指定ID“ diabetes_type-other”,则每次用户选中或取消选中“其他类型”按钮时,应用都必须执行一些代码。 The code only must be executed when the user clicks on this radio button or when the user clicks on any other button unchecking a previous "Other type" checked button. 当用户单击此单选按钮或用户单击任何其他按钮而不选中先前的“其他类型”选中的按钮时, 必须执行代码。 If user clicks on DM1 and then clicks on DM2 the function shouldn't be executed. 如果用户单击DM1,然后单击DM2,则不应执行该功能。 This is my approach: 这是我的方法:

$("#" + _var).change(function() {
    alert('changed');
});

Problem here is the code is the function is triggered when user clicks on "Other type" but not when user unchecks this. 这里的问题是代码是用户单击“其他类型”时触发的功能,而不是用户取消选中时触发的功能。

Other approach is to obtain radio name and then do something like this: 另一种方法是获取无线电名称,然后执行以下操作:

_radioName = "diabetes_type";  // Obtain programmatically
$("input[name=" + _radioName + "]").change(function() {
    alert('changed');
});

Now the problem is this function is triggered always, on any button click and I need the function to be triggered only when user checks or unchecks the given radio button. 现在的问题是,始终在单击任何按钮时都触发此功能,并且我只需要在用户选中或取消选中给定的单选按钮时才触发该功能。 You can play with this jsfiddle . 您可以玩这个jsfiddle

Fiddle : http://jsfiddle.net/sn7eU/1/ 小提琴http : //jsfiddle.net/sn7eU/1/
Examine this code, it should meet your wishes: 检查此代码,它应该符合您的要求:

var _radioName = "diabetes_type";  // Obtain programmatically
var _var = "diabetes_type-other";

#(document).ready(function(){
    var lastChecked = false;
    $("input[name=" + _radioName + "]").change(function() {
        if(this.id == _var) lastChecked = true;
        else if(lastChecked){
           /*The radio input isn't "other". Check if the last checked element equals
             "other" (lastChecked == true). If true, set lastChecked to false*/
            lastChecked = false;
        } else return; /*The input isn't "other", and the last checked element isn't 
                         "other". Return now. */

        alert('changed');
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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