Here is the task at hand:
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:
$(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?
});
});
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.
});
});
$(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();
});
});
form
is a jQuery object, not a function, so form('.pagy-page')
won't work. You need to call the .find()
method:
form.find('.pagy-page').val(pageNum);
You cannot submit a disconnected form. Try this:
form.appendTo($(this)).submit();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.