如何在data-bind中设置图像的click属性

Mat*_*att 1 knockout.js

视图:

<td style="white-space: nowrap;">
   <img data-bind="attr: { onclick: PlaySounds }" src="/Images/audioGreen.png" alt="Pronounce word" title="Pronounce word" style="cursor: pointer" />
   <a data-bind="attr: { href: GoogleQuery }" target="_blank">
      <img src="/Images/googleIcon.png" alt="Google Search" title="Google Search" style="cursor: pointer" />
   </a>
</td>
Run Code Online (Sandbox Code Playgroud)

淘汰视图型号:

function DictionaryEntry() {
   var self = this;
   self.Simplified = ko.observable("");
   self.Traditional = ko.observable("");
   self.Phonetic = ko.observable("");
   self.Definition = ko.observable("");

   self.GoogleQuery = ko.computed(function () {
       return "http://www.google.com/search?q=" + self.Simplified();
   }, self);

   self.PlaySounds = ko.computed(function () {
       return "playSounds('" + self.Phonetic() + "')";
   }, self);
}
Run Code Online (Sandbox Code Playgroud)

关于"attr"绑定的信息:" http://knockoutjs.com/documentation/attr-binding.html "

错误详情:

Microsoft JScript运行时错误:无法解析绑定.消息:ReferenceError:'PlaySounds'未定义; 绑定值:attr:{onclick:PlaySounds}

不知道我哪里错了.如果可能的话,直接绑定而不使用ko.computed值将是一个奖励.任何一种解决方案都将非常感激.

Col*_*inE 5

您不需要使用attr绑定将函数绑定到单击,为此您应该使用Knockout click绑定:

<img data-bind="click: playSounds" src="/Images/audioGreen.png"
     alt="Pronounce word" title="Pronounce word" style="cursor: pointer" />
Run Code Online (Sandbox Code Playgroud)