【问题标题】:jquery to act as href while passing ajax valuejquery 在传递 ajax 值时充当 href
【发布时间】:2015-01-26 17:49:54
【问题描述】:

情况如下:

我需要将 php 脚本加载到模式窗口中。为了做到这一点,我需要将加载模式窗口的按钮设置为一个带有指向该脚本的 href 的标签。然而,在加载该脚本之前,我需要 Ajax 将信息发送到该 php 脚本以运行查询。该查询的结果是该模式窗口中显示的内容。我遇到的问题是单击按钮时,href 请求和 ajax 请求都被处理并且页面返回为空(查看控制台,看到两个 xhr 请求,一个带有 Ajax 信息,另一个带有名称的php文件(我认为是href请求))。为 $_POST 做了一个 var_dump 并得到数组 size=0

这里是按钮代码:

   a href='extschedule.php' type='submit' id='editButton' class='btn btn-primary btn-sm user editButton'  data-toggle='modal' data-target='#editModal' data-id='$id'

这是我的 ajax 代码:

    $('.editButton').click(function(e){

    var getid = $(this).data('id');

    $.post('extschedule.php', {getid: getid});
    e.preventDefault();

这是我选择按钮时控制台中的内容:

 XHR Loaded (extschedule.php - 200 OK - 3.119945526123047ms - 953B)
VM1660:3 **http://localhost/extschedule.php?getid=146**
VM1661:3 Object {startedDateTime: "2015-01-26T17:26:59.392Z", time: 3.119945526123047, request: Object, response: Object, cache: Object…}
VM1663:3 XHR Loaded (extschedule.php - 200 OK - 13.669967651367188ms - 1.077KB)
VM1664:3 **http://localhost/extschedule.php**
VM1665:3 Object {startedDateTime: "2015-01-26T17:26:59.398Z", time: 13.669967651367188, request: Object, response: Object, cache: Object…}

我对 ajax 和 php 相当陌生,但我的猜测是对同一个 php 文件的两个请求都会导致结果出现问题。同样,我需要传递所选按钮的值,同时还需要通过 href 将 php 脚本加载到模态窗口中。有人可以帮我解决这个问题或提出不同的策略来实现这个目标吗?

附: - 我考虑过编辑 href 的 url,以根据 data-id 值将 getid 设置为变量。它适用于第一个,但如果我退出模式窗口并单击另一个用户,它将不会加载新值。

【问题讨论】:

    标签: javascript php jquery html ajax


    【解决方案1】:

    要防止 ajax 请求和提交都发生,请将 type="submit" 更改为 type="button" 并忘记 href。

    在您的示例中,onclick 事件似乎已经在向“extschedule.php”发出请求,如果您正在进行 ajax 调用,您不想提交表单。

    提交将控制权转移到页面的另一个实例,然后 ajax 向服务器发出请求,服务器将结果返回给调用页面。

    【讨论】:

      猜你喜欢
      • 2020-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-24
      • 2013-02-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多