我希望在使用GET
方法提交表单时重新加载部分页面。以下JavaScript运行,但我不确定如何从表单中获取提交的URL?
我原以为var link = jQuery(this).attr('GET');
或者某些人会回复它,并试过URL
等等......但是没有运气。有什么想法吗?
jQuery('form').submit(function(e){
e.preventDefault();
var link = jQuery(this).attr('GET'); //Get the href attribute
alert(link);
history.pushState('data', '', link);
jQuery('#search_main_section').fadeTo(300,0.3,function(){
load(link + ' #search_main_section', function(){ jQuery("#loader").hide();
jQuery('#search_main_section').fadeTo(100,1, function(){});
});
});
答案 0 :(得分:3)
var link = jQuery(this).prop('action');
如果您需要表单的值,则需要使用serialize()
使用动作和加载searlize的示例。
$("#form1").on("submit", function(e) {
e.preventDefault();
var form = $(this);
var action = form.attr("action") || window.location.href;
var formValues = form.serialize();
//example with get
//$.get(action, formValues, function(){ alert("done"); });
//example with load
var url = action + "?" + formValues;
$.load(url + ' #search_main_section', function(){ jQuery("#loader").hide();
});
答案 1 :(得分:0)
<form action="something.php" method="POST">
</form>
jQuery('form').submit(function(e){
e.preventDefault();
var url = $(this).attr('action');
....// rest of code