2013-04-03 43 views
1

我發現這個enter link description here anwser,但它對我沒有好處。敲除檢查綁定不會改變jquery「change」事件的模型值

我希望能夠更改模型值而不觸發「點擊」事件。所以現在當用戶改變值(不是小提琴)時,一些代碼會產生「改變」事件。但是模型並沒有改變它的價值。

所以我有一個

型號

function AppViewModel() { 
    //Common 
    this.TimeType = ko.observable(0); 
    } 

$(document).ready(function() { 
    viewModel = new AppViewModel();  
    ko.applyBindings(viewModel, $("#testId")[0]); 

    $("#change").click(function() { 
     $("#third").attr('checked', 'checked'); 
     $("#third").change(); 
     alert(viewModel.TimeType()); 
    }); 
}); 

HTML

<div id="testId" class="holder main-holder"> 
     <div class="holder"> 
      <input checked="checked" data-bind="checked: TimeType" id="Time" name="Type" type="radio" value="0"><label for="Time" class=" ">First</label> 
     </div> 
     <div class="holder"> 
      </div><input data-bind="checked: TimeType" id="TimeWithTypeDay" name="Type" type="radio" value="1"><label for="TimeWithTypeDay">Second</label> 
     </div> 
     <div class="holder"> 
      <input id="third" data-bind="checked: TimeType" id="TimeWithDaysOfWeek" name="Type" type="radio" value="2"><label for="TimeWithDaysOfWeek" >Third</label> 
     </div> 
    </div> 
      <input id="change" type="button" value ="Change"/> 

小提琴:

http://jsfiddle.net/skiff/4Wnmu/3/

+1

你能請詳細說明'我想有改變模型值,而不觸發能力「單擊」 event.'? – gaurav

+0

我有一個js的UI框架,它處理點擊和觸發更改事件。我不能改變它。所以我需要有能力查看視圖中的用戶更改模型 –

回答

1

在敲除與單選按鈕檢查綁定,該屬性應該等於單選按鈕的值。值始終是字符串。檢查這個工作小提琴:

Working Fiddle

JS

var vm = new AppViewModel(); 

$("#change").click(function() { 
    vm.TimeType("2"); 
    alert(vm.TimeType()); 
}); 

function AppViewModel() { 
    this.TimeType = ko.observable("1"); 
} 

ko.applyBindings(vm); 
+0

它應該是所有視圖模型的解決方案。因此代碼中的某處觸發了「更改」。我無法將viewModel添加到此代碼中。我只能看到,發生了什麼變化。 –

0

That是什麼喲你在找什麼?

viewModel.TimeType.subscribe(); 
+0

您的解決方案沒有幫助。 http://jsfiddle.net/skiff/tWPf8/ –