問題:這是手頭的任務(wù):我們有一個(gè)表格。此表單中有一個(gè)隱藏的輸入字段,其名稱和 ID 為“頁(yè)面”我有分頁(yè)鏈接。提交表單后,結(jié)果將被分頁(yè)。這些鏈接的 href 屬性中包含頁(yè)碼。單擊分頁(yè)鏈接時(shí),我想從分頁(yè)鏈接中提取頁(yè)碼。(這存儲(chǔ)在 pageNumber 變量中),并用這個(gè) pageNumber 替換我表單中隱藏字段的值,然后提交表單(不跟蹤單擊的分頁(yè)鏈接)。這是可能嗎?任何指針將不勝感激。這是我到目前為止的代碼:(注意我使用的是 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? }); });
Jquery - 如何替換選定元素的值然后提交
侃侃爾雅
2021-11-12 16:34:42