繁体   English   中英

在 Rails 中使用“f.input 集合”的 onChange 方法

[英]onChange method with “f.input collection” in Rails

我有这个下拉列表:

<%= f.input :window_type, collection: @window, selected: @window.first, label: "Window Type", input_html: {onChange: "changePlaceholder(this)", type: "text"} %>

我想在您在那里更改选项时捕获,因此我可以使用 javascript 更改占位符。 所以我做了这个 function 来检查我是否收到了预期值:

function changePlaceholder(this){
  console.log("This is " + JSON.stringify(this));
}

但是当我尝试运行它时,它给出了“this”变量的错误,它说的是意外令牌。 在我不得不将 C 从 onChange 更改为 caps 之前,因为它不是那样的,所以也许我在“this”中遗漏了其他东西。

我可以使用 select_chosen,但我更喜欢 f.input,因此我可以控制 controller 中的参数名称。 谢谢。

最后答案很简单,这样的工作。 您必须将 class 添加到输入中,例如:

<%= f.input :topic1, collection: @inputs,include_blank: false,label: "First Input To Read From (Referenced as F)",input_html: {class: "topic1", type: "text" } %>

然后,使用 jquery:

$(document).on("change", "select.topic1", function(e){
  var topic1 = $(this).children("option:selected").val();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM