![](/img/trans.png)
[英]Ruby On Rails: Using collection_select value in the same form
[英]Using RoR form_for with collection_select
在form_for @estimate中,我正在使用collection_select給我一個“潛在客戶”表中潛在客戶名稱的下拉列表
<%= form_for(@estimate) do |f| %>
<div class="field">
<%= f.label "Lead" %><br>
<%= f.collection_select :lead_id, @leads, :id, :full_name, prompt: true %>
</div>
...
select標記的選項已正確填充了線索。 我正在使用jQuery的.getJSON方法從所選選項中檢索數據
$(document).on('change', 'select#estimate_lead_id', function(e) {
var url = "/leads";
var data = {
id: $(this).val()
};
$.getJSON(url, data, function (data, status) {
if (status === 200) {
return data
console.log(data);
alert("THIS IS WORKING!");
};
});
});
但是,我無法使它正常工作。 這是我的服務器日志-它似乎正在訪問數據庫。
Started GET "/leads?id=23" for 127.0.0.1 at 2016-07-02 23:29:29- 0500
Processing by LeadsController#index as JSON
Parameters: {"id"=>"23"}
Lead Load (1.1ms) SELECT "leads".* FROM "leads"
Rendered leads/index.html.erb within layouts/application (10.4ms)
User Load (0.7ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 LIMIT 1
Completed 200 OK in 51ms (Views: 47.6ms | ActiveRecord: 1.8ms)
我正在從線索數據庫中選擇一個ID為23的名稱,但沒有任何日志記錄到控制台,也沒有警報。 我想使用數據填充估算表中的字段。 我究竟做錯了什么?
處理異步任務時,我之前遇到過同樣的問題。 我要做的是分別調用onchange函數,我在select標記內調用它:
它看起來像這樣:
<select onchange="myfunction(parameterIfneeded)">
....
</select>
然后在我的js函數中:
function myfunction(param) {
..............
.......
//PROCESS your AJAX here
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.