doutan2111 2014-07-02 15:50
浏览 46

表单提交即使是'return false' - AJAX [关闭]

I've been following this tutorial from Youtube!, and it just don't want to work the way I want it to.

I got this code in my index.php:

<form action="scripts/keys.php" class="ajax" method="post">
    <p>Key:</p>
    <input type="text" name="key" class="key_input" autocomplete="off" autofocus />
    <input type="submit">
</form> 

This code in my main.js:

$('form.ajax').on('submit', function(){
    var that = $(this),
        url = that.attr('action'),
        type = that.attr('method'),
        data = {};

    that.find('[key]').each(function(index, value){ 
        var that = $(this),
            name = that.attr(''),
            value  that.val('');

        data[name] = value;
    });

    $.ajax({
        url: url,
        type: type,
        data: data,
        success: function(response) {
            console.log(response);
        }
    });
    return false;
});

and finally this in my keys.php:

if(isset($_POST['key'])) {
    echo 'lol';
}

For some reason when I submit the form it still sends me to keys.php, and I don't understand that, when I used return false.

Can someone explain what I'm doing wrong? Don't tell me the correct code, just what I need to change :)

  • 写回答

1条回答 默认 最新

  • donglu3184 2014-07-02 15:52
    关注

    Syntax error, missing equalsign

    that.find('[key]').each(function(index, value){ 
        var that = $(this),
            name = that.attr(''),
            value  that.val('');  // HERE
    //           ^^
        data[name] = value;
    });
    

    And change the PHP to

    if(isset($_POST['key'])) {
        echo 'lol';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能