栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

根据Rails 3中另一个collection_select的选定项目自动填充text_fields

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

根据Rails 3中另一个collection_select的选定项目自动填充text_fields

您要做的是将ajax调用路由到控制器,该控制器将使用包含信息的json进行响应。然后,您将使用jquery填充不同的字段。

在您的路线中:

get "invoice/:id/get_json", :controller=>"invoice", :action=>"get_json"

在您的invoice_controller中:

def get_json  invoice = Invoice.find(params[:invoice_id])  render :text => invoice.to_jsonend

在您的帐单模型中(如果默认的to_json方法不足够):

def to_json  json = "{"  json += "id:'#{self.id}'"  json += ",date_created:'#{self.date}'"  ...      //add other data  you want to have here later  json += "}"end

在您的Javascript文件中,

$("#invoice_selecter").change(function(){  //calls this function when the selected value changes  $.get("/invoice/"+$(this).val()+"/get_json",function(data, status, xhr){  //does ajax call to the invoice route we set up above    data = eval(data);  //turn the response text into a javascript object    $("#field_1").val(data.date_created);  //sets the value of the fields to the data returned    ...  });});

您可能会遇到一些问题,如果您不在google chrome上,我强烈建议下载并安装fire
bug。如果是,请确保使用的是开发工具。我相信您可以通过右键单击并单击检查元素来打开它们。通过这种方式,您应该能够监视ajax请求,它是否成功以及其他情况。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/406812.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号