简体   繁体   English

我可以从其他页面加载表格吗?

[英]Can I load a form from another page?

Is it possible, if so how, for JS to load a form from one page and show it in another? 如果可以的话,JS是否可以从一个页面加载表单并在另一页面显示表单? For example, clicking a button on the '/note/index' page and it reveals a form found in the '/note/new' but i am still in the '/note/index' page. 例如,单击“ / note / index”页面上的按钮,它将显示在“ / note / new”中找到的表单,但我仍在“ / note / index”页面中。

I know I can do this by putting all the html in the index page. 我知道我可以通过将所有html放在索引页中来做到这一点。

Further questions: Is it recommended to do what I ask? 进一步的问题:是否建议按照我的要求去做?

my current HTML for /notes/index.html.erb 我当前的/notes/index.html.erb HTML

<script>
    $(function() {

  // contact form animations
  $('#newNote').click(function() {
    $('#newNoteForm').fadeToggle();
  })
  $(document).mouseup(function (e) {
    var container = $("#newNoteForm");
    if (!container.is(e.target) // if the target of the click isn't the container...
        && container.has(e.target).length === 0) // ... nor a descendant of the container
    {
        container.fadeOut();
    }
  });

});
</script> 

<!--New Note-->
<div class="row first-content-row">
    <div class="col-xs-3">
        <p><%= notice %></p>
        <div class ="col-sm-offset-1 btn btn-lg btn-success">
        <div id = "newNote">New Note</div>
        </div>
    </div><!--End col-->
</div><!--End row -->

<!-- This is the note that will be appearing -->
<div id="newNoteForm">
  <h1>Add a new Note</h1>  
    <%= form_for @session_note, url: {action: "create"}, html: {class: "nifty_form"} do |f| %>
      <%= f.text_area :note, size: "60x12" %>
      <%= number_field(:session_note, :session_id) %>
      <%= f.submit "Save" %>
    <% end %>
</div>

It's possible with AJAX, if your using jquery the code could be: 使用AJAX可能,如果使用jquery,则代码可能是:

$.get('/newpageurl/', function(data) {
    var otherPage = $(data); //Get the other page dom as data
    var otherPageForm = $(otherPage).find('form#myform'); // find the form
    $('body').append('otherPageForm');// place the form in the current page
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM