【问题标题】:Radio button with iCheck only change value when confirm return true带有 iCheck 的单选按钮仅在确认返回 true 时更改值
【发布时间】:2015-05-20 11:06:24
【问题描述】:

我使用iCheck 创建现代单选按钮,有四个选项。当用户更改单选按钮时,我会弹出一条确认消息“您确定吗?”

如果确认返回false,我想返回上一个单选按钮,但现在它不返回上一个单选按钮。

这是代码的一部分:

<form action="#" method="post">
    <div style="padding-left:20px; line-height:2;">
      <input type="radio" id="main-category1" name="main-category" value="1" > 1. <br>
      <input type="radio" id="main-category2" name="main-category" value="2" > 2. <br>
      <input type="radio" id="main-category3" name="main-category" value="3" > 3. <br>
      <input type="radio" id="main-category4" name="main-category" value="4" > 4. <br>
      <input type="radio" id="main-category5" name="main-category" value="5" > 5. <br>
    </div>
  </form>

<scirpt>
$('input').on('ifClicked', function(event){        
   msg = confirm('Are you sure?');
   var cual= this;
   if(msg == false) {
       $('#main-category3').iCheck('check'); // It's check to new value and check to 3rd radio, but I want to check only 3rd radio. 
   }   
});
</script>

【问题讨论】:

  • 如果添加return false会怎样?
  • @JamesDonnelly; add return false --> 没有发生

标签: javascript jquery icheck


【解决方案1】:
<script>
    $('input').on('click', function(event) {
      msg = confirm('Are you sure?');
      var cual = this;
      if (msg == false) {
        $('#main-category3').attr("checked",true);
        // It's check to new value and check to 3rd radio, but I want to check only 3rd radio. 
      }
    });
  </script>

我为您的代码创建了一个 plunk 并进行了更正

http://plnkr.co/edit/gist:1986619?p=preview

【讨论】:

  • ifClicked 是 iCheck 插件中的有效事件。
  • @JamesDonnelly 谢谢你提供的信息。欣赏它。 Plunk 没有提到它。我现在添加它。
猜你喜欢
  • 2012-03-28
  • 1970-01-01
  • 2013-09-14
  • 2013-10-19
  • 1970-01-01
  • 1970-01-01
  • 2013-06-21
  • 2018-08-15
  • 1970-01-01
相关资源
最近更新 更多