doufei16736 2017-03-17 11:15
浏览 163
已采纳

$ .post函数不起作用,没有错误[关闭]

I want to retrieve data from a mySQL server by using jQuery, which triggers a PHP script to receive the data. However, the $.post function from jQuery does not work at all and corrupts all the code, and I don't understand why.

Here is my JS code:

$('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);
        }
    }
});

I included the jQuery library in my main HTML page with this:

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

The $.post message corrupts the entire code, nothing works. When I remove it, the first part of the code works as it should. I have the feeling this is extremly easy to fix but I can't wrap my head around why it does not work. Thank you!

  • 写回答

2条回答 默认 最新

  • douxing6434 2017-03-17 11:19
    关注

    You are not closing parentheses of $.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);
            }
        }
    });
    

    try this :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失