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

问题:

这是手头的任务:

  1. 我们有一个表格。此表单中有一个隐藏的输入字段,其名称和 ID 为“页面”

  2. 我有分页链接。提交表单后,结果将被分页。这些链接的 href 属性中包含页码。

  3. 单击分页链接时,我想从分页链接中提取页码。(这存储在 pageNumber 变量中),并用这个 pageNumber 替换我表单中隐藏字段的值,然后提交表单(不跟踪单击的分页链接)。

这是可能吗?任何指针将不胜感激。

这是我到目前为止的代码:

(注意我使用的是 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?                

       });

    });


侃侃尔雅
浏览 124回答 1
1回答

慕哥9229398

修复表单替换问题form是一个 jQuery 对象,而不是一个函数,所以form('.pagy-page')不会工作。您需要调用该.find()方法:form.find('.pagy-page').val(pageNum);为什么不提交克隆表格?您不能提交断开连接的表单。尝试这个:form.appendTo($(this)).submit();
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript