Javascript js按钮上的条件活动单击事件

Javascript js按钮上的条件活动单击事件,javascript,jquery,knockout.js,Javascript,Jquery,Knockout.js,只有在满足bool条件时,我才需要按钮上的函数。目前,我已经实现了类似于 <button class="btn btn--primary pull-right" type="submit" data-bind="click: myBoolFlag ? $root.myFunction : function(){}"/> 我在多个页面上使用这个按钮作为我的通用提交按钮,但在其中两个页面上,它只需要在其正常提交功能的点击上添加一个小功能 我觉得通过匿名空函数执行此操作不是最好的方法

只有在满足bool条件时,我才需要按钮上的函数。目前,我已经实现了类似于

<button class="btn btn--primary pull-right" type="submit" data-bind="click: myBoolFlag ? $root.myFunction : function(){}"/>

我在多个页面上使用这个按钮作为我的通用提交按钮,但在其中两个页面上,它只需要在其正常提交功能的点击上添加一个小功能


我觉得通过匿名空函数执行此操作不是最好的方法,我正在寻找更干净的替代方法。

如果不希望函数在单击时执行,请尝试返回true

<button class="btn btn--primary pull-right" type="submit" data-bind="click: myBoolFlag ? $root.myFunction : return true;"/>

在这种情况下,我通常会这样做

HTML

<!-- I gave it an ID to make it unique -->
<button id="myButton" class="btn btn--primary pull-right" type="submit"/>

这只会产生一个sytanx错误。我必须在匿名函数中写入返回true,这实际上根本不会改变解决方案。然后您可以将参数传递给函数,然后决定。类似这样的东西-单击:function(){$root.myFunction(myBoolFlag);}可能是Sweet的副本,很高兴提供帮助!
<!-- I gave it an ID to make it unique -->
<button id="myButton" class="btn btn--primary pull-right" type="submit"/>
//this handles the click function now
$('#myButton').click(function() {
  if(condiion1){
   do this;
  } else {
    do that;
  } 
});