[英]Jquery - How to replace a selected element's value and then submit
这是手头的任务:
这是可能吗? 任何指针将不胜感激。
这是我到目前为止的代码:
(注意我使用的是 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?
});
});
这是更新的代码:
$(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
是 jQuery object,而不是 function,所以form('.pagy-page')
不起作用。 您需要调用.find()
方法:
form.find('.pagy-page').val(pageNum);
您不能提交断开连接的表单。 尝试这个:
form.appendTo($(this)).submit();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.