【问题标题】:IE10 event :(change) with confirm does not reset value to previous valueIE10 事件:(更改)确认不会将值重置为以前的值
【发布时间】:2016-02-17 20:54:37
【问题描述】:

我的 Internet Explorer 有问题(目前是 v 10)我有 2 个单选按钮是/否和一个确认框,如果用户单击取消,该值应该恢复到原始值

例如:yes 是默认值。用户单击否,弹出显示 - 用户取消应返回是。 问题...值没有改变它停留在否

我的代码在 FireFox 和 GoogleChrome 上运行良好

见jsFiddle(我认为你必须在IE10中打开它......)

enter code here

https://jsfiddle.net/grosjambon/nxy0g1cg/

如果jsfiddle不工作请评论

EDIT1

在这里找到了一些有用但有一点问题的东西。当我尝试以 Knockout 在 Click Binding 中执行的方式将多个参数绑定到函数时,当“人员”确认更改值时,单选按钮中的值不会更新(两个单选按钮内部都没有)

HTML

<input class="radio-inline" type="radio" data-bind="click: function(data,event){confirmCheck2(lang('Options1'),false, '1Group0', data, event)}, checked: IsSomething,checkedValue: true" />
 <input class="radio-inline" type="radio" data-bind="click: function(data,event){confirmCheck2(lang('Options1'),true, '1Group0', data, event)}, checked: IsSomething,checkedValue: false" />

ViewModel(Knockout) Ok confirm 有问题的方法(两个radio都是空的)

 m.confirmCheck2 = function (propertyName, inputValue, stepName, data, event) {
                console.log(event);
                if (!confirm("Are you sure?")) {
                    event.stopImmediatePropagation();            
                    return false;
                }
                return true;
            }

有效但没有额外属性的方法

html

 <input class="radio-inline" type="radio" data-bind="click: confirmCheck, checked: IsITARAndCGP,checkedValue: true" />  

视图模型

m.confirmCheck = function (data, event) {
                console.log(data);
                console.log(event);
                if (!confirm("Are you sure?")) {
                    event.stopImmediatePropagation();
                    return false;
                }
                return true;
            }

EDIT 2 所以在回复之间,我找到了使用 Roy 的旧答案的解决方案。 P.S 如果我们使问题复杂化:所以我有多个属性 m.Property1 m.Property2,m.Property3。当 1 为 True 时,所有其他均为 false,如果 True 属性变为 False,则所有内容均为 false。只有 1 个值可以为真。

因此,当您更改值时,使用 RoyJ 的先前回答,它将触发每个具有 Confirm Popup 的属性的订阅,因为所有 3 个属性都会触发相同的确认。

    var showPopup = true; 
    m.Property1.subscribe(function () {
                var inputValue = m.Property1();
                if (showPopup) {
                    if (confirm(f.lang("Message"))) {
                        showPopup = false;//Disable showPopup because we set value inside resetValueFunction
                        resetValueWithCondition("Property1"), inputValue);
                    } else { // if cancel
                        showPopup = false;
                        m.Property1(!inputValue);
                        return true;//Exit code
                    }
                }
                showPopup = false;//Disable showPopup because we set value inside resetValueFunction
                resetValueWithCondition("Property2", inputValue);
                showPopup = true;

            });
            m.Property2.subscribe(function () {
                var inputValue = m.Property2();
                if (showPopup) {
                    if (confirm(f.lang("Message"))) {
                        showPopup = false;//Disable showPopup because we set value inside resetValueFunction
                        resetValueWithCondition("Property2", inputValue);
                    } else { // if cancel
                        showPopup = false;
                        m.Property2(!inputValue);
                        return true;//Exit code
                    }
                }

                showPopup = true;

            });

函数resetValueWithCondition

function resetValueWithCondition(propertyName,inputValue) {
                if (!m.Property1() && !m.Property2()) {//In this If I hide every field in my form and set Property 1 and Property2.... to false.
                    //m.Step1Visiblity(false);  
                    resetFormValueAfterStepPosition('1Group0');//Reset all form field      
                    setPropertyValue(false, false);//Here is simply m.Property1(false) etc...
                } else {
                    resetFormValueAfterStepPosition('1Group0');//Reset all form field
                    if (inputValue == true) {//Only perform this method if Property goes to True, otherwise don't execute (otherwise it trigger the confirm box multiple time when I Set Propertyvalue
                        m.visibilityLogic(propertyName);//Set PropertyValue to either true,false or false,true if is it property1 or property2
                    }

                }

            }

【问题讨论】:

  • 这是错的小提琴,因为它与你的问题无关
  • 现在应该好了?我编辑了一个链接。

标签: javascript internet-explorer knockout.js


【解决方案1】:

您对表单的关注过多,而对模型的关注不够。不要摆弄与单选按钮相关的事件,而是使用您的 observables。在这种情况下,您需要在数据项和前端之间设置一个层来检查确认。

var ViewModel = function() {
  var self = this,
    savedHS01 = ko.observable(true);

  self.HS01 = ko.computed({
    read: savedHS01,
    write: function(newValue) {
      if (confirm("Are you sure?")) {
        savedHS01(newValue);
      }
      self.HS01.notifySubscribers();
    }
  });
  self.HS02 = ko.observable();
};

ko.applyBindings(new ViewModel());
body {
  font-family: arial;
  font-size: 14px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script>
<div>
  <input class="radio-inline" type="radio" data-bind="checked: HS01, checkedValue:true" />
  <span class="form-control-inline">yes</span>

  <input class="radio-inline" type="radio" data-bind="checked: HS01, checkedValue:false" />
  <span class="form-control-inline">no</span>
  <br/>Hs01:
  <input data-bind="value: HS01" />
  <br/>ShouldBe:
  <input data-bind="value: HS02" />
</div>

【讨论】:

  • 嗨 Roy,当我点击否时工作正常,但如果我点击否,确认框不会显示 --> 好的,然后点击是(确认框应该会弹出)。 (因为 !inputvalue 的 true = false。如果我编辑代码,它会在取消事件时进入无限循环
  • 我以为您只想确认从 Yes 更改为 No。您需要使用 Protected Observables 之类的东西。 knockmeout.net/2011/03/…
  • 我已经更新了示例代码,使其表现得更像受保护的 observable:暴露的 observable 是一个可写计算,等待确认写入“真实” observable。
  • 嗨,罗伊,请参阅edit2,我研究了您的旧答案以使某些工作正常进行。没有简化的问题(见edit2)......我会尝试更新的答案,看看它是否有效
  • 处理无线电组情况的常用方法是使用selectedItem 指示选择了哪些可能的属性。每个项目的真假值可以从selectedItem == this 计算出来。
猜你喜欢
  • 1970-01-01
  • 2021-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多