簡體   English   中英

將變量從PHP傳遞到jQuery click()函數

[英]Pass variable from PHP to jQuery click() function

有人可以告訴我如何將變量Y傳遞給$('a:eq(0)').click()嗎? 我需要提醒服務器響應.click()函數。 如果重要的話,我正在使用PHP框架。

<script>
    $(document).ready(function() {
        function clockIn(str){
            if (window.XMLHttpRequest)
            { // code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp=new XMLHttpRequest();
            }
            else
            {// code for IE6, IE5
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    document.getElementById("message").innerHTML=xmlhttp.responseText;
                    var y = xmlhttp.responseText ; 

                }
            }
            xmlhttp.open("GET","http://localhost/gasstation/index.php/welcome/check_time_clock/"+ str,true);
            xmlhttp.send();
        } 

        $( "input:submit, a", ".login" ).button();

        $("a:eq(0)").click(function(){
            clockIn(<?php echo $emp_id; ?>);
        });

        $("a:eq(1)").click(function(){
            alert('m') ;
        })
    });
</script>

由於您正在進行ajax調用,因此必須等到服務器響應后才能在click hanlder中獲取y的值。 您可以將處理程序傳遞給clockIn ,然后在ajax響應到來之后執行它。 嘗試這個。

function clockIn(str, callback){
   ...
   xmlhttp.onreadystatechange=function()
   {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      {
        document.getElementById("message").innerHTML=xmlhttp.responseText;
        var y = xmlhttp.responseText ; 
        callback(y);
      }
   }
   ...
}


$("a:eq(0)").click(function(){
   clockIn("<?php echo $emp_id; ?>", function(y){
      //You will get the value of y here.
      alert(y);
   });
});

由於您使用的是jQuery,因此應完全利用它來使您的代碼簡單干凈。 無需創建自己的代碼來創建xmlhttp請求對象和bla bla,您可以使用jQuery的$.ajax來實現相同的用途。 您的代碼將變成這樣。

function clockIn(str, callback){
   $.ajax({
      url: "http://localhost/gasstation/index.php/welcome/check_time_clock/"+ str,
      success: function(data){
         $("#message").html(data);
         callback(data);
      }
   });
}

嘗試這個:

$("a:eq(0)").click(function(){
        clockIn('<?php echo $emp_id; ?>');

    });

可能會有所幫助

function clockIn(str, cb) {
    $.get("http://localhost/gasstation/index.php/welcome/check_time_clock/"+ str), function(res) {
        // do your stuffs here.
        var y = res;
        cb && cb(y);
    }
}

$("a:eq(0)").click(function(){
    clockIn("hello", function(y){ alert(y); }); // your y here
});

自從您使用jQuery以來,我會做這樣的事情:

<script>
$(document).ready(function() {
    var clockIn = function (str) {
        $.ajax({
            url: "http://localhost/gasstation/index.php/welcome/check_time_clock/"+ str,
            success: function (data) {
                $("#message").html(data);
                var y = data;
            }
        });
    }

    $( "input:submit, a", ".login" ).button();

    $("a:eq(0)").click(function(){
        clockIn('<?php echo $emp_id; ?>');
    });
    $("a:eq(1)").click(function(){
        alert('m') ;
    });

});
</script>

我希望這可以幫助你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM