简体   繁体   English

AJAX请求未提交我的表格

[英]AJAX request not submitting my form

I have an ajax request which is used to submit a form. 我有一个用于提交表单的ajax请求。

$('#position'+i).change(function () { 
        var data = $(this).attr('value');
        var form = $('#pages_form');
        $.ajax({
            type: "POST",
            url: form.attr('action'),
            data: data,
            succes: alert('Postion has been saved')
        });
    }); 

It dosent seem to want to go the the url weather I hard code it or use the form action attribute. 它似乎不想使用我硬编码的url天气或使用form action属性。 What do I need to do to get this to submit my form. 我需要做什么才能提交此表格。

Its weird because if I hit enter it will submit the form but it will bypass the ajax request. 这很奇怪,因为如果我按Enter键,它将提交表单,但会绕过ajax请求。

var form = $('pages_form');

This is bad, what sort of selector is this, theres no pages_from tag, as far as I know. 这很不好,据我所知,这是一种什么样的选择器,没有pages_from标记。 It should be 它应该是

var form = $('#pages_form');

if its id. 如果其ID。

If class then 如果上课的话

 var form = $('.pages_form');

That's not how the call back works also you had success misspelled. 如果您成功拼错了电话,那也不是回叫的工作方式。 You pass a function name or an anonymous function to the success method. 您将函数名称或匿名函数传递给成功方法。

Working version: 工作版本:

$('#position'+i).change(function () { 
        var data = $(this).attr('value');
        var form = $('#pages_form'); // Is this an id?
        $.ajax({
            type: "POST",
            url: form.attr('action'),
            data: data,
            success: function () {
               alert('Postion has been saved');
            }
        });
    }); 

Your problem most likely lies with the selector you use to get the form: 您的问题很可能与用于获取表格的选择器有关:

var form = $('pages_form');

There is no html tag with the name pages_form ;) 没有名称为pages_form html标签 ;)

Also your way of specifying the callback is just plainly wrong. 同样,您指定回调的方式显然是错误的。

A couple of corrections: 一些更正:

var form = $('pages_form');

is not referring to a valid element. 不是指有效的元素。 If 'pages_form' is the id of your form, then you need to use this: 如果“ pages_form”是表单的ID,则需要使用以下代码:

var form = $('#pages_form');

Also, you spelled wrong 'success', and it doesn't have the format needed. 另外,您拼写错误的“成功”,并且它没有所需的格式。 Change that line to 将该行更改为

success: function() {alert('Postion has been saved');}

I think after those are fixed your code should work. 我认为在这些问题解决之后,您的代码应该可以工作了。 :) :)

Ok, then a few validations are needed, although they may seem obvious. 好的,尽管看起来很明显,但仍需要进行一些验证。

  1. Make sure i contains a value and that there's an input element with the corresponding id, so if i equals 1, then you have an input element with the id 'position1'. 确保我包含一个值,并且有一个带有相应ID的输入元素,因此,如果我等于1,则您有一个ID为“ position1”的输入元素。
  2. Make sure your form has the correct action attribute correctly set. 确保您的表单具有正确设置的正确的动作属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM