【问题标题】:Jquery show div if query returns data如果查询返回数据,则 Jquery 显示 div
【发布时间】:2016-02-19 11:13:09
【问题描述】:

我正在使用一个脚本,该脚本需要显示一个包含小 FORM 的 DIV。只有当对数据库的查询返回 1 或更多的结果时,我才需要显示 FORM。

我有一个用于查找数据的 MySQL 查询,它正在工作并填充一个名为 $alerts 的 var,然后我回显 var $alerts 的内容。

我的 jQuery 脚本如下所示:

$(document).ready(function() { 
        function runrequests() { 
           $.ajax({ 
             url: 'check_request_events.php',
             type:"POST",
             data:"alerts", 
             success: function(data){ //4
             document.getElementById("alerts").innerHTML = data;

               //render the dynamic data into html
             } 
           });  
        };  

               // run it initially
        runrequests();

        // run it every 30 seconds
        setInterval(runrequests, 30 * 1000);
        });

显示div的脚本部分是:

<div id="alerts">
<form id="form1" name="form1" method="post" action="<?php echo $editFormAction; ?>">
<input type="image" src="../../../../conf_images/butler_request.png" name="button" id="button" value="Submit" />
<input name="RecordID" type="hidden" value="<?php echo $ClientRecordId;?>" />
<input type="hidden" name="MM_update" value="form1" />
<input type="hidden" name="RecordID" value="<?php echo $row_ConfAlert['RecordID']; ?>" />
</form>

当查询返回大于0的数据时,我需要做什么才能只显示DIV。

非常感谢您的宝贵时间和帮助。

新代码 jQuery

$(document).ready(function() { 
        function runrequests() { 
           $.ajax({ 
             url: 'check_request_events.php',
             type:"POST",
             data:"alerts", 
             success: function(data){

                if (data.length > 0){
                    $('#alerts').show();
                } else {
                    $('#alerts').hide();
                }
               //render the dynamic data into html
             } 
           });  
        };  

               // run it initially
        runrequests();

        // run it every 30 seconds
        setInterval(runrequests, 30 * 1000);
        });

CSS

#alerts {
display:none;
}

DIV

<div id="alerts">
<form id="form1" name="form1" method="post" action="<?php echo $editFormAction; ?>">
<input type="image" src="butler_request.png" name="button" id="button" value="Submit" />
<input name="RecordID" type="hidden" value="<?php echo $ClientRecordId;?>" />
<input type="hidden" name="MM_update" value="form1" />
<input type="hidden" name="RecordID" value="<?php echo $row_ConfAlert['RecordID']; ?>" />
</form>
</div>

仍然没有隐藏 DIV。 问候

【问题讨论】:

  • 你不能用简单的if吗?相信你一定学过
  • if (data.length &gt; 0){ //exec code here } 将是最简单的解决方案,但这取决于您的数据返回的格式。
  • 大家好,我已经使用了 if (data.length > 0){ document.getElementById("alerts").innerHTML = data;} 并且它工作到一定程度。 DIV 仅显示 1 或 0,而不显示 DIV 内的表单。有什么想法吗?
  • 而不是 "document.getElementById("alerts").innerHTML = data;"尝试使用 "$('#alerts').show();" (我假设 id "alerts" 有一个 display:none; 在 css 中)

标签: jquery


【解决方案1】:

使用给定的代码。数据类型是 json。 check_request_events.php 中需要的 json 编码数据。试试这个。

$(document).ready(function() { 
        function runrequests() { 
           $.ajax({ 
             url: 'check_request_events.php',
             type:"POST",
             data:"alerts",
 dataType: 'json',
             success: function(data){ //4
if (data.length > 0) {
            document.getElementById("alerts").innerHTML = data;

        }else{
            //alert('no data');
        }

               //render the dynamic data into html
             } 
           });  
        };  

               // run it initially
        runrequests();

        // run it every 30 seconds
        setInterval(runrequests, 30 * 1000);
        });

【讨论】:

  • 大家好,我在尝试使其正常工作时仍然遇到问题。我已经发布了新的更新代码。谁能看到我哪里出错了。
  • 大家好,已解决。我用过: if (data == '1'){ //NOTE '1' $('#alerts').show(); } 其他 { $('#alerts').hide(); }
【解决方案2】:

在执行 if 语句时,所缺少的只是数据周围的 ''。

$(document).ready(function() { 
    function runrequests() { 
       $.ajax({ 
         url: 'check_request_events.php',
         type:"POST",
         data:"alert",
         dataType: 'json',
            success: function(data){ 

                if (data == '1'){
                    $('#alerts').show();
                } else {
                    $('#alerts').hide();
                }
            } 
        });  
    };  

           // run it initially
    runrequests();

    // run it every 30 seconds
    setInterval(runrequests, 30 * 1000);
});

【讨论】:

    猜你喜欢
    • 2023-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多