简体   繁体   English

Jquery - 如何替换选定元素的值然后提交

[英]Jquery - How to replace a selected element's value and then submit

The Problem:问题:

Here is the task at hand:这是手头的任务:

  1. We have have a form.我们有一个表格。 This form has a hidden input field in it with the name and id of "page"此表单中有一个隐藏的输入字段,其中包含“页面”的名称和 ID
  2. I have pagination links.我有分页链接。 When the form is submitted, the results are paginated.提交表单后,结果将分页。 The links have the page numbers contained in their href attribute.链接的页码包含在其 href 属性中。
  3. When a pagination link is clicked, I want to extract the page number from the pagination link.单击分页链接时,我想从分页链接中提取页码。 (this is stored in the pageNumber variable), and replace the hidden field's value in my form with this pageNumber, and then submit the form (without following through on the pagination link that was clicked). (这存储在 pageNumber 变量中),并用这个 pageNumber 替换我表单中隐藏字段的值,然后提交表单(不遵循单击的分页链接)。

Is this at all possible?这是可能吗? Any pointers would be much appreciated.任何指针将不胜感激。

Here is the code I have so far:这是我到目前为止的代码:

(Note I am using rails, and this is contained in the application.js file: (注意我使用的是 rails,它包含在 application.js 文件中:

  $(document).on('turbolinks:load', function(event){        
  var ransack_form = $('form').clone();    // clone the form on page load.
                                           // form is passed into the function.
                                           // <input type="hidden" name="page" id="page" value="4" class="pagy-page">
  $(document).on('click', 'a.page-link', {form: ransack_form}, function(event){  
    var pageNumber = $(this).attr("href");    
    var form = event.data.form;
                // The form has a hidden input element with id and name being: "gage"
                // we want to replace the hidden input's value with the pageNumber value
                // obtained from above. How can this be done?
    form('.pagy-page').val(pageNum);    
    form.submit();  
                // We want to prevent the pagination link from being clicked.
                // event.preventDefault() prevents this from happening 
                // but we also want the form to be submitted at the same time!
                // is there a way around this?                
       });
    });

Updated Code:更新代码:

  1. Thanks to Barmar we are at the stage where we now have a fully edited form that we need to submit.多亏了 Barmar,我们现在有了一个需要提交的完全编辑过的表单。
  2. When the pagination URL link is clicked, we want the form to be submitted WHILE ALSO preventing the browser from following the URL link.单击分页 URL 链接时,我们希望提交表单,同时阻止浏览器跟踪 URL 链接。 For some reason, the form is not being submitted:由于某种原因,没有提交表单:

Here is the updated code:这是更新的代码:

    $(document).on('turbolinks:load', function(event){        
  var ransack_form = $('form').clone();    // clone the form on page load.
                                           // form is passed into the function.
                                           // <input type="hidden" name="page" id="page" value="4" class="pagy-page">
  $(document).on('click', 'a.page-link', {form: ransack_form}, function(event){  
    var pageNumber = $(this).attr("href");    
    var form = event.data.form;             
    form.find('.pagy-page').val(pageNumber);    
    form.submit();  
    event.preventDefault();
                // We want to prevent the pagination link from being clicked.
                // event.preventDefault() prevents this from happening 
                // but we also want the form to be submitted at the same time!
                // How can we submit the form (and allow the page to be refreshed)
                // while at the same time preventing the url to be clicked.
       });
    });

The Final Solution:最终的解决方案:

$(document).on('turbolinks:load', function(event){        
  var ransack_form = $('form').clone();    // clone the form on page load.
                                           // form is passed into the function.
                                           // <input type="hidden" name="page" id="page" value="4" class="pagy-page">
  $(document).on('click', 'a.page-link', {form: ransack_form}, function(event){  
    var pageNumber = $(this).attr("href");    
    var form = event.data.form;             
    form.find('.pagy-page').val(pageNumber);              
    form.appendTo($(this)).submit();    
       });
    });


Fixing The Form Replacement Problem修复表单替换问题

form is a jQuery object, not a function, so form('.pagy-page') won't work. form是 jQuery object,而不是 function,所以form('.pagy-page')不起作用。 You need to call the .find() method:您需要调用.find()方法:

form.find('.pagy-page').val(pageNum);

Why Won't a Cloned Form be submitted?为什么不提交克隆表单?

You cannot submit a disconnected form.您不能提交断开连接的表单。 Try this:尝试这个:

form.appendTo($(this)).submit();

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

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