从外部触发淘汰赛事件

从外部触发淘汰赛事件

本文介绍了从外部触发淘汰赛事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以html形式具有以下Select-Element:

I have the following Select-Element in an html-form:

<select multiple="multiple" data-bind="options: candidateList, optionsValue: 'id', optionsText: 'title', optionsAfterRender: setOptionTitle, selectedOptions: selectedCandidates, optionsAfterRender: setOptionTitle, event: { dblclick: addSelectedCandidate, change: candidateChanged }, enable: enabled()">
<option title="first" value="1">first</option>
<option title="second" value="2">second</option>
<option title="third" value="3">third</option>
</select>

现在,我使用jQuery方法设置选项已选择"-值来选择该选择的多个元素.

Now I select multiple Elements of that Select using jQuery-Methods setting the "Option SELECTED" - value.

如您所见,select上面有一个数据绑定,它来自敲除.该敲除代码在其他人提供的另一个JavaScript文件中.我们不能真正在那里更改内容.而且我们的代码不是敲除代码,而是简单的jQuery代码.

As you can see, that select has a data-binding on it, which is from knockout. That knockout-code is in another JavaScript-File provided by someone else. We cannot really change content there. And our code is NOT knockout-, but simple jQuery-Code.

现在我的问题是,该选择上有验证.当我手动单击某个元素时,这将启用另一个Button等.

Now my problem is, that there are validations on that select. When I click onto an element manually, this enables another Button etc.

但是当我尝试通过代码执行此操作时,什么也没有发生.我尝试插入选定",$(option).trigger('click')$(option).click()$(option).trigger('change')$(option).change();

But when I try to do this by code, nothing happens. I tried inserting the "selected", $(option).trigger('click'), $(option).click(), $(option).trigger('change') and $(option).change();

有没有办法强制淘汰赛来识别"我们以编程方式更改的内容?

Is there any way to force knockout to "recognize" the stuff we change programmatically?

推荐答案

使用val,然后使用trigger('change'),它将起作用.这是一个演示:

Use val and then trigger('change') and it'll work. Here's a demo:

ko.applyBindings({
  candidateList: [{ id: 1, title: "first" }, { id: 2, title: "second" }, { id: 3, title: "third" }],
  setOptionTitle: function() { },
  selectedCandidates: ko.observableArray(),
  addSelectedCandidate: function() { },
  candidateChanged: function() { },
  enabled: ko.observable(true)
});

function getRandomVal() { return (Math.floor(Math.random() * (3 - 1)) + 1).toString(); }

window.setInterval(function() {
  var vals = [];
  if (Math.random() > 0.75) { vals.push(getRandomVal()); }
  if (Math.random() > 0.75) { vals.push(getRandomVal()); }
  if (Math.random() > 0.75) { vals.push(getRandomVal()); }
  $("select").val(vals).trigger("change");
}, 1000);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.0/knockout-min.js"></script>

<select multiple="multiple" data-bind="options: candidateList, optionsValue: 'id', optionsText: 'title', optionsAfterRender: setOptionTitle, selectedOptions: selectedCandidates, optionsAfterRender: setOptionTitle, event: { dblclick: addSelectedCandidate, change: candidateChanged }, enable: enabled()">
<option title="first" value="1">first</option>
<option title="second" value="2">second</option>
<option title="third" value="3">third</option>
</select>

<hr>
Selected candidates: <code data-bind="text: ko.toJSON($root.selectedCandidates, null, 2)"></code>

PS.如果您无法更改实际上可能应该去的代码,则可能主要是政治问题.像这样混合使用jQuery和KO在短期内会伤害您,从长远来看会严重伤害您.

PS. You have mostly a political problem probably, if you cannot change the code where your changes probably actually should go. Mixing jQuery and KO like this will hurt you in the short run, and it will hurt you badly in the long run.

PPS.所发布的KO代码中有一些奇怪的东西(至少没有上下文).首先,它具有option,但是应该生成.其次,侦听change事件,但是通常选择预订或可写的计算将是更好的选择.第三,optionsAfterRender被声明两次.最后,执行enabled值,如果可以观察到的话,这是多余的.

PPS. There's a few weird things (without context at least) in that KO code as posted. First, it has options in it, but those should be generated. Second, the change event is listened to, but typically a subscription or writeable computed would be a better choice. Third, the optionsAfterRender is declared twice. Lastly, the enabled value is executed, which is superfluous if it would be an observable.

这篇关于从外部触发淘汰赛事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 15:10