weixin_33686714 2015-12-03 23:57 采纳率: 0%
浏览 57

Ajax if语句请求

$("#Submit").click(function(event){
    event.preventDefault();
    var th = '<tr><th>' + "Business" +'</th><th>' + "Address"+ '</th><th>'+ "Rating" + '</th><th>' + "Date" + '</th></tr>';
    $("#restaurants").empty().html(th);
    var Searching = $("#Search").val();
    $.ajax({
        type     : "GET",
        url      : "http://www.cs.kent.ac.uk/people/staff/lb514/hygiene/hygiene.php",
        dataType : "json",
        data     : {op : "searchname", name : Searching},
        success  : function(data){
            $.each(data,function(key,results){
                if(results.length > 1){
                    $("#restaurants").append(
                        "<tr><td>" + results.business +"</td>"+
                        "<td>" + results.address + "</td>" + 
                        "<td>" + results.rating + "</td>" + 
                        "<td>" + results.date + "</td></tr>"
                    )
                }else if( results.length  <  1){
                    alert("The search term ");
                }
            })
        }
    });
})

This is an AJAX request, called after the user press the submit button to search for a restaurant. Now for a better design I included if statement to check whether the callback object is empty but it doesnt work.
However, even with this I have tested and it works if you give a similar name to a restaurant. But it doesnt alert if you give a name that is not in the database and I don't see where I went wrong.

PS. I also tried data.length

  • 写回答

1条回答 默认 最新

  • weixin_33720452 2015-12-04 00:22
    关注

    It's often helpful to have a complete working example, so here is my go. The following gives you a section to respond if your have 0 results. Although this doesn't cover all use cases as it's not 100% obvious how the php file you're accessing works.

    <HTML>
    <HEAD>
    </HEAD>
    <BODY>
        <input type="text" id="Search">
        <button id="Submit">Submit</button>
        <div id="restaurants"></div>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
        <script>
        $("#Submit").click(function(event){
                    event.preventDefault();
                    var th = '<tr><th>' + "Business" +'</th><th>' + "Address"+ '</th><th>'+ "Rating" + '</th><th>' + "Date" + '</th></tr>';
                    $("#restaurants").empty().html(th);
                    var Searching = $("#Search").val();
                    $.ajax({
                     type : "GET",
                     url : "http://www.cs.kent.ac.uk/people/staff/lb514/hygiene/hygiene.php",
                     dataType : "json",
                     data : {op : "searchname", name : Searching},
                     success : function(data){
                         if(data.length == 0){
                             // what to do when nothing returns
                             alert("Nothing found");
                         }else{
                            $.each(data,function(key,results){
                                if(results.length > 1){
                                 $("#restaurants").append(
                                             "<tr><td>" + results.business +"</td>"+
                                             "<td>" + results.address + "</td>" + 
                                             "<td>" + results.rating + "</td>" + 
                                             "<td>" + results.date + "</td></tr>")
                                }
                            });
                        }
                    }});
                  });
        </script>
    </BODY>
    

    评论

报告相同问题?

悬赏问题

  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊