【问题标题】:issue with jquery .post() not workingjquery .post() 无法正常工作的问题
【发布时间】:2011-05-17 15:48:48
【问题描述】:

所以我是使用 jquery .post() 的新手,但是我没有使用我以前没有使用过的方法。

我试图在单击按钮时发布两个隐藏的输入值:

$('#button').live('click', function() {
    $.post('export_file.php', { group: form.group.value , test: form.test.value },
    function(output)    {
        $('#return').html(output).show();
    });
});

我已经测试了按钮事件是否成功触发,目前我在export_file.php 中尝试做的只是回显某些东西。

这是我的表格:

<form name="form">
<input type="hidden" name="group" value="<?echo $group;?>">
<input type="hidden" name="test" value="<?echo $test_id;?>">
<input type="button" class="Mybutton" id="button" name="btnSubmit" value="Export Results">
</form>

我在原始页面上找到了我的 div:

&lt;div id='return'&gt;&lt;/div&gt;

export_file.php:

<?php

echo "whatever, something!";

?>

谁能指出我哪里出错了。非常感谢,

【问题讨论】:

  • Firebug 控制台告诉你什么?任何 JS 错误?
  • 查看我在回答中添加的演示

标签: php jquery .post


【解决方案1】:

试试:

$('#button').live('click', function() {
    $.post('export_file.php', { group: $("input[name='group']").val() , test: $("input[name='test']").val() },
    function(output)    {
        $('#return').html(output).show();
    });
});

【讨论】:

  • 这将使它适用于带有name=test任何输入和带有name=group的任何输入
  • @Neal:他现在的表格只有一个。来吧,为什么这必须如此复杂?不要假设不存在的要求。
  • @mellamokb 我编写了我认为 OP 将来会做的事情。
  • @Neal,在这种情况下,您为什么不也为他验证输入?为什么你还没有添加自动完成功能?
  • 事实上,你为什么不为他编写一个小插件,使用序列化的表单数据来ajax提交他喜欢的任何表单?我的意思是你肯定“相信”他将来会想通过 AJAX 提交表单,不是吗?
【解决方案2】:

修正这一行:

$.post('export_file.php', { group: form.group.value , test: form.test.value },

把它改成这样:

var group_val = $('input[name="group"]', 'form[name="form"]').get(0).value;
var test_val = $('input[name="test"]', 'form[name="form"]').get(0).value;
$.post('export_file.php', { group: group_val , test: test_val },

小提琴:http://jsfiddle.net/maniator/cQ2vZ/

【讨论】:

  • @downVoter 有人想解释一下吗?
【解决方案3】:

我已在您的 HTML 中为您的表单元素添加了 ID:

<form name="form">
    <input type="hidden" name="group" id="group" value="<?echo $group;?>">
    <input type="hidden" name="test" id="test" value="<?echo $test_id;?>">
    <input type="button" class="Mybutton" id="button" name="btnSubmit" value="Export Results">
</form>

然后修改 jQuery 以通过 ID 从这些字段中获取值,并在 AJAX 调用的参数中使用这些值:

$('#button').live('click', function() {
    var groupValue = $("#group").val();
    var testValue = $("#test").val();

    $.post('export_file.php', { group: groupValue , test: testValue },
    function(output)    {
        $('#return').html(output).show();
    });
});

【讨论】:

  • 他们不需要身份证,请看下面我的回答
  • 你在作业中缺少.val()var groupValue = $("#group").val();
  • 你的意思是上面的答案:P
  • @Neal 各位,你的回答太复杂了,在这么简单的情况下几乎没有必要设置变量
  • @Neal,确实他们不需要 id 来工作,但 Id 选择器是迄今为止最快的,所以我总是建议人们尽可能使用它,而不是 INPUT[name=foo]。归根结底,他们都在工作,这只是个人喜好。
【解决方案4】:

试试这个

$('#button').live('click', function() {
    var group_val = $("input[name='group']").val(); // gets the value of hidden field with the name group
    var test_val = $("input[name='test']").val(); // gets the value of hidden field with the name test and store it in test_val variable
    $.post('export_file.php', { group: group_val  , test: test_val  },
    function(output)    {
        $('#return').html(output).show();
    });
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 2011-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    相关资源
    最近更新 更多