提交时出现Jquery语法错误

Jquery syntax error on submit

本文关键字:语法 错误 Jquery 提交      更新时间:2024-04-17

我正试图提交一个表单,该表单已由用户通过jquery添加了字段,问题是提交表单时通常会漏掉这些用户定义的字段。我在jquery中使用了submit函数,但收到了错误。我在下面做错了什么吗?

$(document).ready(function () {
    $('#form').on('submit', function(e) {
        //prevent the default submithandling
        e.preventDefault();
        //alert(1);
        //send the data of ''this'' (the matched form) to yourURL
        $('processme.php', $(this).serialize()).submit;
    });
});

我以前尝试过他的post函数,但因为它实际上没有进入post上的页面,所以导致了一个500服务器错误,这是可行的,但它在firebug控制台中的串行数据上引发了语法错误。

感谢您的帮助

编辑:

这正是错误"错误:语法错误,无法识别的表达式:details1=&userval=2/jquery-1.9.1.js4421行throw new Error("语法错误,无法识别的表达式:"+msg);"

以下是用于添加新输入字段的代码:

function addRow(){
 $('#tbl1').find('tbody:last').after('<tr><td>User Defined</td><td><input name="userdef[]" value="" type="text"></td>/td></tr>');
}

p.S processme.php内置了一个转发器,该转发器取决于验证响应。所以帖子需要提交到processme.php。

PHp代码:

 print_r($_POST);
 print_r($_POST("userdef")); echo "<br/>";
        foreach ($_POST("userdef") as $key=>$val) {
            echo $key." - ".$val.", ";
        }
        exit()

输出:

  Array ( [userdef] => Array ( [1] => 11111 ) [userval] => Array ( [0] => ) ) 

我认为最好的方法是使用内置的submit函数。

我的猜测是,字段要么没有正确添加到表单中,要么没有正确命名。

如果你能发布该部分的代码,我们可能会让它发挥作用。

试试这个,看看原始提交的东西是否正常工作

var count = 0;
function addRow(){
    count++;
    $('#tbl1').find('tbody:last').after('<tr><td>User Defined</td><td><input         name="userdef['+count+']" value="" type="text"></td>/td></tr>');
}

foreach ( $_POST['userdef'] as $key => $val){
    echo $key . ' = ' . $val . ''n'; 
}

试试这个方法:

$('#form').on('submit',function() {
    var url = "processme.php";
    $.ajax({
           type: "POST",
           url: url,
           data: $(this).serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert(data); // show response from the php script.
           }
         });
    return false; // avoid to execute the actual submit of the form.
});

我认为$('processme.php', $(this).serialize()).submit是问题所在。你的意思是做$.get,而.submit是无用的。

$(document).ready(function () {
    $('#form').on('submit', function(e) {
        //prevent the default submithandling
        e.preventDefault();
        //alert(1);
        //send the data of ''this'' (the matched form) to yourURL
        $.get('processme.php', $(this).serialize());
    });
});