Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/83.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果用户没有';不接受JS确认消息?_Javascript_Jquery_Ruby On Rails_Ruby_Radio Button - Fatal编程技术网

Javascript 如果用户没有';不接受JS确认消息?

Javascript 如果用户没有';不接受JS确认消息?,javascript,jquery,ruby-on-rails,ruby,radio-button,Javascript,Jquery,Ruby On Rails,Ruby,Radio Button,我正在使用RubyonRails3.2.2和jquery-Rails-2.0.2。在我的表单中,我有许多单选按钮,如果用户不接受确认消息,我想检查之前选择的单选按钮。我想我可以实现如下内容 <%= form.radio_button(:name_1, 'value_1', { :onclick => ('selectRadio(this)') }) %> <%= form.radio_button(:name_2, 'value_2', { :onclick =>

我正在使用RubyonRails3.2.2和jquery-Rails-2.0.2。在我的表单中,我有许多单选按钮,如果用户不接受确认消息,我想检查之前选择的单选按钮。我想我可以实现如下内容

<%= form.radio_button(:name_1, 'value_1', { :onclick => ('selectRadio(this)') }) %>
<%= form.radio_button(:name_2, 'value_2', { :onclick => ('selectRadio(this)') }) %>
<%= # Other radio buttons %>
<%= form.radio_button(:name_n, 'value_n', { :onclick => ('selectRadio(this)') }) %>


<script type="text/javascript">
  function selectRadio(radiobutton) {
    var confirm_message = confirm('Are you sure?');

    if (confirm_message) {
      # Check the selected radio button.
    } else {
      # Re-check the previous selected radio button.
    }
  }
</script>
('selectRadio(this)})%%>
('selectRadio(this)})%>
('selectRadio(this)})%>
功能选择单选(单选按钮){
var confirm_message=confirm('你确定吗?');
如果(确认消息){
#选中选中的单选按钮。
}否则{
#重新选中上一个选中的单选按钮。
}
}

。。。但我没有办法“重新检查以前选择的单选按钮”。可能吗?如果是这样,如何做到这一点?

一个解决方案是在用户检查时存储用户的选择,并在必要时将其还原回来。

请尝试下面的内容

<script type="text/javascript">
   // first get checked radio button where myform is your form in which radiobuttons reside  
   var lastRadio = $("#myform input[type='radio']:checked");
   function selectRadio(radiobutton) {
      var confirm_message = confirm('Are you sure?');
      if (confirm_message) {
         lastRadio = $(radiobutton);
         # Check the selected radio button.
      } else {
         lastRadio.attr('checked',true);
         # Re-check the previous selected radio button.
      }
    }
</script>

//首先检查单选按钮,其中myform是单选按钮所在的表单
var lastRadio=$(“#myform输入[type='radio']:选中”);
功能选择单选(单选按钮){
var confirm_message=confirm('你确定吗?');
如果(确认消息){
lastRadio=$(radiobutton);
#选中选中的单选按钮。
}否则{
lastRadio.attr('checked',true);
#重新选中上一个选中的单选按钮。
}
}

您可以使用以下代码块执行此操作

我假设您考虑的所有单选按钮都有一个公共类,如
收音机

var prev = $(".radios:checked");

$(".radios").change(function() {
    if(confirm('Are you sure?')){ 
           prev = $(this); 
    }else{
           prev.prop('checked','checked');   
    }    
});​

参考: