$ .post函数不起作用,没有错误

时间:2017-03-17 11:15:15

标签: javascript php jquery html mysql

我想通过使用jQuery从mySQL服务器检索数据,这会触发PHP脚本来接收数据。但是,jQuery中的$.post函数根本不起作用并破坏了所有代码,我不明白为什么。

这是我的JS代码:

$('button#btnSubmit').on('click', function() {
    //Compare button pressed
    var sel1 = $('select#country1').val(); //country iso codes
    var sel2 = $('select#country2').val();

    if(sel1 === "placeholder1" || sel2 === "placeholder2" || sel1 === sel2) {
        alert("Please select at least two different countries")
    }
    else {
        //post this to php file to retrieve data
        try {
            $.post('ajax/retrieve_data.php', { sel1: sel1, sel2: sel2 }, function(data) {
                $('div#test-data').text(data);
        }
        catch(err) {
            alert(err.message);
        }
    }
});

我在我的主HTML页面中包含了jQuery库:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>

$.post消息会破坏整个代码,无效。当我删除它时,代码的第一部分工作正常。我觉得这很容易修复,但我无法解决为什么它不起作用。 谢谢!

2 个答案:

答案 0 :(得分:2)

您没有关闭$ .post

的括号
$('button#btnSubmit').on('click', function() {
    //Compare button pressed
    var sel1 = $('select#country1').val(); //country iso codes
    var sel2 = $('select#country2').val();

    if(sel1 === "placeholder1" || sel2 === "placeholder2" || sel1 === sel2) {
        alert("Please select at least two different countries")
    }
    else {
        //post this to php file to retrieve data
        try {
            $.post('ajax/retrieve_data.php', { sel1: sel1, sel2: sel2 }, function(data) {
                $('div#test-data').text(data);
           });
        }
        catch(err) {
            alert(err.message);
        }
    }
});

试试这个:)

答案 1 :(得分:1)

在回调函数之后,您的代码中缺少var step = 280; var scrolling = false; $(".scrollUp").bind("click", function (event) { event.preventDefault(); $("#timeline").animate({ scrollTop: "-=" + step + "px" }); }) $(".scrollDown").bind("click", function (event) { event.preventDefault(); $("#timeline").animate({ scrollTop: "+=" + step + "px" }); }) $('.timelineButton').click(function () { $('#timeline').animate({ scrollTop: $($(this).attr('href')).offset().top }, 2000); return false; });

}
相关问题