English 中文(简体)
如果在 Jquery 中用其它语句调用函数时, 如何在内部调用函数
原标题:how to call a function inside if else statement in Jquery
  • 时间:2012-05-21 15:13:24
  •  标签:
  • jquery

我有一个jquery message box 的代码。 如果我点击下面的链接, 就会看到一个消息框 。

<p><a id="msgup" class="">Demo Top</a></p>
<script>
    $("#msgup").bar({
        color            :  #1E90FF ,
        background_color :  #FFFFFF ,
        removebutton     : false,
        message          :  Your profile customization has been saved! ,
        time             : 2000
    }); 
</script>

现在我想做的是当我得到“冷”作为我的 Ajax 服务器响应中的值时显示信息框。 为了实现这一点,我尝试了以下代码, 但它不起作用。 可能是因为它没有调用 jquery 函数 。 请您如何使它生效?

$.ajax({  
    type: "POST",
    url: "<?php echo base_url(); ?>contents/hello",
    data: "id="+a_href,
    success: function(server_response) {
        if (server_response ==  cold ) {

        //Beginning of the code for message box
        $("#msgup").bar({
            color            :  #1E90FF ,
            background_color :  #FFFFFF ,
            removebutton     : false,
            message          :  Your profile customization has been saved! ,
            time             : 2000
        });
        //End of the code for message box

        // Instead of the message box code I have tried 
        // alert( Message ); and it worked
        }                       
        else {
            $("#result").html(server_response);
        }               
    }                              
}); //$.ajax ends

提前感谢:)

最佳回答
$("#msgup").bar(…)

将点击事件与 #msgup 绑在一起( 只是猜测), 所以从ajax 代码中调用它不会有帮助。 实现您目标的一个可能办法是初始化 $.bar 并触发从 ajax 代码的点击, 也许这样:

<p><a id="msgup" class="">Demo Top</a></p>
<script>
    $("#msgup").bar({
        color            :  #1E90FF ,
        background_color :  #FFFFFF ,
        removebutton     : false,
        message          :  Your profile customization has been saved! ,
        time             : 2000
    });
    $.ajax({
        type: "POST",
        url: "<?php echo base_url(); ?>contents/hello",
        data: "id="+a_href,
        success: function(server_response){
            if (server_response ==  cold ){
                //Beginning of the code for message box
                $("#msgup").click();
                //End of the code for message box
           } else {
              $("#result").html(server_response);}                   
           }
        }                             
    });   
</script> 
问题回答

您可以将 alert () connoole.log () 调用 alert (server_ reponse) > 。 调用 alert (server_ reponse) 调用 。 您也可以看到 AJAX 添加错误是否正确返回 : 条件( 以其自己的函数调用如成功 : ) 。

我猜你会想要检查 而不是, 对于字面无效,甚至对象无效, 只要它进入成功功能。

{... {...}如果(服务器_回复 =========================================================================================================== ======================================================================================= ========================================= ===============================================

一旦你发现你期望它如何回来, 你可以只用你需要的支票。

您是否尝试过将它包在设置时间( 函数) {${( "#msgup" ) 中 ).bar () ;} }, 100 中 ; 来检查它是否是一个时间问题? 如果是这样的话, 而不是将其置于成功之中 : 也许将其完整 : @ info/ plain

另外,如果在如果 () 和它被调用后,您在其中放置了警报,那么当时(又回到计时计时) 时有 ID #msgup 的 HTML 元素就是 HTML 元素。

Looking at jquery.bar.js, $("#msgup").bar(…) is just initialize element "#msgup" for click event binding. If click event is fired, the jquery.bar plug in will create new element and show it as message box. So, you need to fire click event. Try this

$.ajax({  
    type: "POST",
    url: "<?php echo base_url(); ?>contents/hello",
    data: "id="+a_href,
    success: function(server_response) {
        if (server_response ==  cold ) {

        //Beginning of the code for message box
        $("#msgup").bar({
            color            :  #1E90FF ,
            background_color :  #FFFFFF ,
            removebutton     : false,
            message          :  Your profile customization has been saved! ,
            time             : 2000
        });
        $("#msgup").click(); //fire click event
        //End of the code for message box

        // Instead of the message box code I have tried 
        // alert( Message ); and it worked
        }                       
        else {
            $("#result").html(server_response);
        }               
    }                              
}); //$.ajax ends




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签