简体   繁体   English

检查先前单击的单选按钮

[英]Check previously clicked radio button

I have a form having multiple radio buttons in different groups. 我有一个在不同组中具有多个单选按钮的表单。 eg category vehicle has two radio buttons 例如类别车辆有两个单选按钮

  1. 2 wheeler 2轮车
  2. 4 wheeler 四轮车

same as other categories have 2-2 radio buttons. 与其他类别相同,有2-2个单选按钮。

What i want to do is when i check 4 wheeler from 2 wheeler i show a warning message pop up, are u sure to switch, if yes then ok, but for no i want to check again 2 wheeler radio button. 我想做的是,当我从2轮车中检查4轮车时,我弹出警告消息,确定要切换,如果是,那么确定,但是不,我想再次检查2轮车单选按钮。 I can not do this from id as well coz i have to do this on other fields as well. 我不能从id做到这一点,因为我也必须在其他字段上做到这一点。

<input type="radio"/>

Your questions is not very clear but from what I understand I guess you want: 您的问题不是很清楚,但是据我了解,我想您想要的是:

$(document).ready(function(){
$('#submit_button').click(function() {
    if (!$("input[@name='radioName']:checked").val()) {
     alert('Nothing is checked!');
    return false;
    }
else {
  alert('One of the radio buttons is checked!');
}
 });
 });

And add the same name tag for all your radio inputs: 并为所有无线电输入添加相同的名称标签:

<input type="radio" name="radioName"/>

This might be what you're trying to achieve: 这可能是您要实现的目标:

var old_input = $('input[name="wheeler"]:checked');
$('input[name="wheeler"]').change(function(e) {
    console.log(e);
    if (
        old_input.next().text() == '2 Wheeler' &&
        $(this).next().text() == '4 Wheeler'
    ) {
        if (confirm('Are you sure you want to change?')) {
            old_input = $(this);
        }
        else {
            this.checked = 0;
            old_input.get()[0].checked = 1;
        }
    }
    else {
        old_input = $(this);
    }
});

http://jsfiddle.net/pQpk7/ http://jsfiddle.net/pQpk7/

I think you are looking for this http://jsfiddle.net/7rF3d/ When a radio button is checked for the first time nothing happens. 我认为您正在寻找的http://jsfiddle.net/7rF3d/首次选中单选按钮时,没有任何反应。 When someone checked another he is first prompted for a confirmation. 当某人检查另一个人时,首先会提示他进行确认。 If the user cancels the old checked radio will be restored. 如果用户取消,将恢复旧的已检查的收音机。

var lastChecked;

$("input[type=radio]").click(function() {

    if(typeof(lastChecked) != "undefined" && !confirm("are you sure?")) {

        $("input[type=radio]:checked").attr("checked", "false");

        $(lastChecked).prop("checked", true);

    } else {

        lastChecked = $('input[type=radio]:checked');

    }

});

Since jQuery isn't tagged in the question, here's a pure JS approach. 由于jQuery没有在问题中标记,因此这是一种纯JS方法。 Guys, seriously, try to quit suggesting jQuery solutions when it isn't even tagged in the question. 伙计们,认真的尝试在问题中甚至没有标记的情况下尝试放弃建议jQuery解决方案。

You can achieve this by using the click event and returning false if there is at least one radio button checked, then the answer to a window.confirm is false, which will prevent it to select the new one as expected. 您可以通过使用click事件并在选中至少一个单选按钮的情况下返回false来实现此目的,然后对window.confirm的回答为false,这将阻止它按预期选择新按钮。

var oneChecked=false;
var elem=document.getElementsByName("wheeler");

for (var i=0;i<elem.length;i++) {
   elem[i].onclick=function(e) {
       if (oneChecked && !window.confirm("Are you sure to switch?")) {
           return false;
       } else {
           oneChecked=true;
       }
   }
}

FIDDLE 小提琴

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

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