onchange显示/更改部分 [英] onchange display/change a partial

查看:103
本文介绍了onchange显示/更改部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用select下拉菜单

There is a drop down using select

<%= select("array", "folder", @rows.keys, {}, :onchange =>"?" )%>

有一个叫做表单"的部分

There is a partial called "form"

<%= render "form"%>

只要选择发生变化,我就需要渲染局部图像.请让我知道是否有办法.我发现remote_function已从rails 3.0中弃用.我在这里看到了所有可能的带有onchange和select标签的链接,但找不到正确的答案.我是Rails,jQuery和Ajax的新手.请帮助

I need to render the partial whenever there is a change in the selection. Please, let me know if there is a way to do it. I found that the remote_function is deprecated from rails 3.0. I have seen all possible links here with onchange and select tags but could not find a proper answer. I'm new to rails, jQuery and Ajax. Please help

谢谢

推荐答案

为什么不在同一页面中呈现表单,并在选择标记更改中隐藏并显示该表单

why not render the form in the same page and hide and show it on change of select tag

<html> 
  <body>

  <!-- Your Select tag -->
  <select id="select_id"> 
    <option value="one" selected> One </option>
    <option value="two"> Two </option>
  </select>

  <!-- Here your a div inside with your form is placed -->
  <div id="myform" style="display:none">
    <form action="/someurl">
      .. ..
    </form> 

   <!-- JQuery code to  show the form -->
  <script type="text/javascript">
    $(document).ready(function() {
       $("select#select_id").bind("change",function() {
         if ($(this).val() == "one") 
          $("div#myform").show(); 
         else
          $("div#myform").hide(); 
       })
   })

  </script>

</html>

我还是很想发出ajax请求

I you still want to make an ajax request I pretty simple too

$(document).ready(function() {
  $("select#select_id").bind("change",function() {
    if ($(this).val() == "one") {
     $.ajax({
        url : "/form_url",
        data : "html", 
        type: "GET",
        success : function(data) {
          $("div#myform").html(data)
        }
     }) 

  })
})

在服务器端,您需要使用format.js响应ajax请求并渲染使用render命令定向的模板,或者仅定义.js.erb文件

on the server side you need to respond_to the ajax request using format.js and render a template directed using render command or just define a .js.erb file

这篇关于onchange显示/更改部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆