2016-02-17 30 views
0

我有一個問題與Internet Explorer(10節現在)我有2個單選按鈕是/否與確認框,如果用戶點擊取消,該值應該回到原來的值IE10事件:與確認(其他城市)不重置值前值

例:yes爲默認值。用戶點擊否,彈出顯示 - 用戶取消應該返回到是。 問題...值不會改變它停留在無

在Firefox和GoogleChrome

我的代碼做工精細

見的jsfiddle(您必須在IE10中打開它,我認爲...)

enter code here 

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

請評論,如果的jsfiddle不起作用

EDIT1

找到東西放在這裏它的工作,但有一個小問題。當我嘗試在道路功能多參數綁定淘汰賽做在點擊綁定,當「人」確認更改值,該值不更新單選按鈕(包括無線電有內他們什麼)

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" /> 
有按下OK鍵確認問題(包括收音機空)

是工作,但沒有額外的屬性

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所以在應答之間,我發現用羅伊的舊答案的解決方案。 P.S如果我們複雜的問題:所以我有多個屬性m.Property1 m.Property2,m.Property3。當1爲真時,所有其他都是假的,如果真實屬性變爲假,則一切都是錯誤的。只有1個值可以是真實的。

因此,與當您更改值RoyJ的以前的答案,這將觸發具有確認彈出,因爲所有的財產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 
        } 

       } 

      } 
+0

那是錯誤的小提琴,原因是其擁有無關,與你的問題 – QBM5

+0

應該是不錯的吧?我編輯了一個Linked。 – Guillaume

回答

1

你付出太多注意形式並不足以模型。而不是擺弄與單選按鈕相關的事件,與您的觀察者一起工作。在這種情況下,您需要在數據項和前端之間建立一個圖層來檢查確認。

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>

+0

嗨Roy,工作正常,當我點擊否,但確認框不顯示,如果我單擊否 - >確定,然後單擊是(確認框應彈出)。 (因爲!inputvalue的true = false。並且如果我編輯它的代碼,它在取消事件 – Guillaume

+0

上進入無限循環我以爲你只想確認從Yes到No的改變。你需要使用類似Protected Observables的東西http: //www.knockmeout.net/2011/03/guard-your-model-accept-or-cancel-edits.html –

+0

我更新了示例代碼,使其更像一個受保護的observable:暴露的observable是一個可寫計算等待確認寫入「真正的」可觀察到的信息 –