简体   繁体   English

如何使用按钮动态更改表单?

[英]How can I use buttons to dynamically change a form?

I have a table that displays all of a users Teacher models. 我有一个表,显示所有用户的教师模型。 I want the rightmost column of the table to contain 'edit' buttons that cause a bootstrap modal to appear with a form that submits to that models edit path. 我希望表格的最右边一栏包含“编辑”按钮,这些按钮会导致引导模态与提交给该模型编辑路径的表单一起出现。

So far I have a table with buttons whose id's correspond to the id's of the model they are displaying. 到目前为止,我有一个带有按钮的表,这些按钮的ID对应于它们正在显示的模型的ID。 When the buttons are clicked, the modal appears, but the form always submits to the same url. 单击按钮时,将出现模式,但表单始终提交相同的URL。 I'm having trouble finding a way to change the url that the form submits to based on the id of the button that was pressed. 我无法找到一种方法来根据按下的按钮的ID更改表单提交的url。

Button: 按键:

<a id="edit-#{teacher_id_here}" href="#myModal" role="button" class="btn" data-toggle="modal">
  Launch demo modal
</a>

Modal: 模态:

<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" 
     aria-labelledby="myModalLabel" aria-hidden="true">
    ... 
</div>

One possibility is to use a custom data attribute (eg data-teacher_id ) in your anchor tag and then retrieve the teacher_id via a callback bound to the click event of the anchor tag. 一种可能性是在锚标记中使用自定义data属性(例如data-teacher_id ),然后通过绑定到锚标记的click事件的回调来获取teacher_id The function of the callback would be to update the form in the modal with the extracted teacher_id (see the documentation on jQuery.data() ) 回调的函数,是更新与所提取的模态形式teacher_id (见文档jQuery.data() )

However, in my own code I use a different approach. 但是,在我自己的代码中,我使用了不同的方法。 I would convert to anchor tag to an "ajaxified" link and fill the #myModal with the form partial returned by the edit action of the TeacherController. 我将锚标记转换为“ ajaxified”链接,并用#myModal的edit操作返回的部分表单填充#myModal。 In that case your myModal division would initially be empty. 在这种情况下,您的myModal分区最初将为空。

The link is set up to point to the right URL and to be handled by Ajax: 链接设置为指向正确的URL,并由Ajax处理:

= link_to "Launch modal", edit_teacher_path(@teacher), :class => "btn ajax-modal-trigger", 
  :data => {:remote => true, :type => 'html'}

Since the link has the data-remote attribute set to true, Rails will automatically submit it via ajax to the right URL (namely, the edit_teacher_path(@teacher) ). 由于链接的data-remote属性设置为true,因此Rails将通过ajax自动将其提交到正确的URL(即edit_teacher_path(@teacher) )。 Now you only need to bind the ajax success event to actually fill the modal with the form and to trigger the modal show: 现在,您只需要绑定ajax成功事件即可以表单实际填充模式并触发模式显示:

 $("body").on('ajax:success', "a.ajax-modal-trigger", function(evt, data, status, xhr){
   $("#myModal").html(data).modal('show');
 });

Of course, in the TeachersController's edit action you need to make sure you handle Ajax requests and respond with the correct form partial (Note that the format is html even though it's an Ajax request. That's because you want to render an html partial): 当然,在TeachersController的edit操作中,您需要确保处理Ajax请求并以正确的形式partial进行响应(请注意,即使它是Ajax请求,其格式也是html 。这是因为您要呈现html局部):

class TeachersContoller
  .....

  def edit
    @teacher = Teacher.find(parame[:id])
    respond_to do |format|
       format.html do
         render :partial => "form", :object => @teacher
       end 
     end
  end
end

You could use this as a general-purpose approach whenever you want to render dynamic modals. 每当您要渲染动态模态时,都可以将其用作通用方法。 The only thing that needs to change is the url that the anchor tag points to. 唯一需要更改的是定位标记指向的网址。

暂无
暂无

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

相关问题 如何使用JavaScript或jQuery更改SharePoint 2013编辑表单中“保存”和“取消”按钮的背景颜色? - How can I use JavaScript or jQuery to change the background color of the `Save` and `Cancel` buttons in a SharePoint 2013 edit form? 在此示例中,如何使用AJAX动态更改跨度文本? - How can I use AJAX to dynamically change span text in this example? 如何在HTML表单中创建两组单选按钮,并且当A组中的更改将在B组中更改时? - How can I create two groups of radio buttons in a HTML form and when change in A Group will change in B Group? 如何使表格和按钮内联? - How can I make form and buttons inline? 如何使用表单上的按钮图像我无法控制HTML? - how can I use images from buttons on a form I have no control over the HTML? 如何将单选按钮更改为Bootstrap按钮 - How can I change radio buttons to Bootstrap buttons 如何在Cordova应用程序中使用按钮? - How I can use Buttons in a cordova application? 如何在HTML中使用按钮来更改 <html> 元件? - How can I use buttons in my HTML to change the class name of the <html> element? 如何使用单个功能来更改网页中所有按钮的innerHTML? - How can i use a single function to change the innerHTML of all the buttons in a webpage? 当按下下一个和上一个按钮时,如何使用javascript和jquery更改文本值? - How can I use javascript and jquery to change a text value when next and previous buttons are pressed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM