问题描述:

Can some one tell me how to pass variable Yto $('a:eq(0)').click()? I need to alert the server respond to the .click() function. I am using a PHP framework if that's important.

<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>

网友答案:

Since you are making an ajax call you have to wait until the server responds so you cannot get the value of y inside click hanlder. You can pass a handler to clockIn and then execute it once the ajax response comes. Try this.

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);
   });
});

Since you are using jQuery you should leverage it completly to make your code simple and clean. Instead of creating write the code your own to create xmlhttp request object and bla bla you can smply use $.ajax of jQuery for the same use. Your code will become like this.

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

Try this:

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

    });

May Be it will Help

网友答案:

I would do something like this since your using 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>

I hope this helps you out

网友答案:
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
});
相关阅读:
Top