简体   繁体   English

序列化并发布表单时,jQuery的隐藏字段值为null

[英]JQuery hidden field value null when form is serialized and posted

I have the following html script: 我有以下html脚本:

<form id="myForm">
...
<input hidden name="myInput" id="myInput">
...
</form>

And somewhere I set a value for the hidden input: 在某个地方,我为隐藏的输入设置了一个值:

$('#myInput').val('a value here');

then, I serialize the form and post it in an url: 然后,我序列化表单并将其发布在url中:

$.post("my_url_here", $("#myForm").serialize())

Where I handle this post, I try to access my hidden input value with: 在处理此帖子的地方,我尝试使用以下命令访问隐藏的输入值:

$_POST['myInput']

but it shows null (empty actually), when I expect it to have the value that I set before. 但当我希望它具有我之前设置的值时,它显示为null(实际上为空)。 If I explicitly set the value like this: 如果我像这样显式设置值:

<input hidden name="myInput" id="myInput" value="explicit_value">

it works fine. 它工作正常。 Any idea what's going on? 知道发生了什么吗?

UPDATE (the whole code, the order is the same as in my real code): UPDATE(整个代码,顺序与我的真实代码相同):

<script>

    jQuery( document ).ready(function( $ ) {
           eventHandler(){
             ...
             $('#myInput').val('a value here');
             myPostFunction();
          }
    });

</script>
<form id="myForm">
    ...
    <input hidden name="myInput" id="myInput">
    ...
</form>
<script>

    jQuery( document ).ready(function( $ ) {
           window.myPostFunction= function () {
             alert($("#myForm").serialize());//It shows myField=&otherField=value
             alert($("#myInput").val());// This shows the correct value.
             $.post("my_url_here", $("#myForm").serialize());
          }
    });

</script>

That should work, but try to compare with mine, and see the result :) 那应该起作用,但是尝试与我的比较,并查看结果:)
You are using PHP right? 您使用的是PHP吗?
Try to create a new php file, named sendAjax.php and type this : 尝试创建一个名为sendAjax.php的新php文件,并输入以下内容:

<!doctype html>
<html>
    <head>
        <title>http://stackoverflow.com/questions/35281545/jquery-hidden-field-value-null-when-form-is-serialized-and-posted</title>
        <script src="jquery-1.7.1.min.js"></script>
    </head>
    <body>
        <form id="myForm">
            <input type="hidden" name="myInput" id="myInput"/>
        </form>
        <script>
            (function(){
                $('#myInput').val('a value here');
                $.post("receiveAjax.php", $("#myForm").serialize(), 
                    function(data){
                        alert(data);
                    });
            })();
        </script>
    </body>
</html>

And create another new php file, named receiveAjax.php, put it into the same folder as sendAjax.php and type this : 并创建另一个新的php文件,名为receiveAjax.php,将其放入与sendAjax.php相同的文件夹中,并输入以下内容:

<?php
    $data = $_POST;
    var_dump($data);
?>

"hidden" should be the value of the "type" attribute, I don't know if it's related but try to replace : “ hidden”应该是“ type”属性的值,我不知道它是否相关,但尝试替换:

<input hidden name="myInput" id="myInput">

By : 由:

<input type="hidden" name="myInput" id="myInput">

OK, thank you all for your help but finally I found the solution. 好的,谢谢大家的帮助,但最终我找到了解决方案。 It was the name of the field that wasn't OK. 这是该字段的名称,不正确。 I just changed the name of the field and it is fine now. 我刚刚更改了字段名称,现在可以了。 It should have been a conflict with the name but I did't realize it since the script executed fine. 它应该与名称冲突,但是由于脚本执行得很好,所以我没有意识到。 After trying all, I thought to change the name... Again, thank you all! 经过全部尝试,我想更改名称...再次谢谢大家!

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

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