我正在尝试创建一个ajax请求,其中URL是提交时的表单。
我的Jquery:
$(document).ready(function() {
$('.menuitem input:checkbox').change(function(){
$.ajax({
type:'get',
url:form.submit();,
data:form.serialize(),
success:function(msg){
$('#formcontent').html(msg);
}
})
})
$('input:submit').hide();
$('.menuitem input:checkbox').change(function(){
if($(this).is(":checked")) {
$('div.hidediv').removeClass("hidediv");
}
});
});
答案 0 :(得分:1)
$('.menuitem input:checkbox').change(function() {
// fetch the form containing the checkbox whose value
// has just changed so that we could infer its action and method
var form = $(this).closest('form');
$.ajax({
url: form.attr('action'),
type: form.attr('method'),
data: form.serialize(),
success: function(msg) {
$('#formcontent').html(msg);
}
});
});
答案 1 :(得分:1)
固定代码
$(document).ready(function() {
$('.menuitem input:checkbox').change(function(){
var form = $(this).parents('form');
$.ajax({
type:'get',
url:form.attr('action'),
data:form.serialize(),
success:function(msg){
$('#formcontent').html(msg);
}
});
$('input[type="submit"]').hide();
$('.menuitem input[type="checkbox"]').change(function() {
if($(this).is(':checked')) {
$('div.hidediv').addClass('someclass'); // not needed it there is another way of identifying the div later to add to hidediv class again.
$('div.hidediv').removeClass('hidediv');
} else {
$('div.someclass').addClass('hidediv');
}
});
});
说明:
form
什么都没有在任何地方定义。form
并且您form.submit();
,则会提交表单(不是您想要的。)答案 2 :(得分:0)
你所在的行:
url: form.submit;,
应该是:
url: form.attr('action'),
所以你最终得到:
$.ajax({
type:'get',
url:form.attr('action'),
data:form.serialize(),
success:function(msg){
$('#formcontent').html(msg);
}
})
您可能还想将“type”值更改为form.attr('method')以保持一致性。