通过参数将传递的函数分配给动态创建的输入类型的onclick事件

Assign a passed function through parameters to the onclick event of a dynamically created input type

本文关键字:创建 动态 输入 类型 事件 onclick 函数 参数 分配      更新时间:2024-06-07

我正在从js.erb文件调用一个coffee脚本:

openModal("<%=escape_javascript render :partial=>"userbill/new" %>", "Check Bills", function(){alert("OK");});

在我的HTML文件中,我有:

<input type= "button" id="left_modal_done" class= "btn btn-large btn-primary" style="margin-top: 5px;" value= "Done"></input>

现在上面的咖啡脚本看起来有点像这样:

@openModal = (html_str, title, otherFunction) ->
  $("#left_modal_done").onclick = otherFunction //Not working

如何将参数化函数(即警报函数)分配给输入类型的onclick事件?

您正在尝试分配jQuery对象的onclick属性,这是没有意义的。

$("#left_modal_done").on('click', otherFunction);

是你想要的行为。