简体   繁体   English

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

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

I have this dropdown:我有这个下拉列表:

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

I want to capture when you change an option there, so i can change a placeholder with javascript.我想在您在那里更改选项时捕获,因此我可以使用 javascript 更改占位符。 So i made this function to check that i was receiving the expected value:所以我做了这个 function 来检查我是否收到了预期值:

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

But when i try to run it, it gives an error with the "this" variable, it says unexpected token.但是当我尝试运行它时,它给出了“this”变量的错误,它说的是意外令牌。 Before i had to change the C from onChange to caps because it was not like that so maybe i'm missing something else with the "this".在我不得不将 C 从 onChange 更改为 caps 之前,因为它不是那样的,所以也许我在“this”中遗漏了其他东西。

I could use a select_chosen, but i prefer the f.input so i can have control over the parameter name in the controller.我可以使用 select_chosen,但我更喜欢 f.input,因此我可以控制 controller 中的参数名称。 Thanks.谢谢。

At the end the answer was simple, something like this works.最后答案很简单,这样的工作。 You have to add a class to the input like:您必须将 class 添加到输入中,例如:

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

And then, with jquery:然后,使用 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