将参数从一个函数传递给另一个函数

时间:2020-09-18 18:33:45

标签: javascript

在这里,我一直在开发chat应用程序。在这里,我想根据用户的chat messages加载email address。在这里,我想将email address传递给后端。

javascript中,我想将电子邮件从LoadChat传递到(".friend").each(function(x){函数。 它显示在console.log函数的LoadChat中,立即变为undefined

这是Javascript代码

     $(".friend").each(function(x){     
          $(this).click(function(){
                        
              var name = $(this).find("p strong").html();
              var email = $(this).find("p span").html();
              var id = $(this).find("p spans").html();
                    
              LoadChat(email);
                                                
              $("#profile p").html(name);
              $("#profile span").html(email);           
              
              $(".message").not(".right").find("img").attr("src", $(clone).attr("src"));                                    
              $('#friendslist').fadeOut();
              $('#chatview').fadeIn();
                 
          setTimeout(function(){
          $('#chatview').fadeOut();
          $('#friendslist').fadeIn();               
       }, 50);            
    });              
   });
}); 

这是另一个LoadChat函数

<script type="text/javascript">
    LoadChat();
    
    setInterval(function () {
        LoadChat();
    }, 1000);

    function LoadChat(email) {
        
console.log(email);
$.post('handlers/messages.php?action=getMessages&email='+email, function (response) {

    var scrollpos = $('#chat-messages').scrollTop();
    var scrollpos = parseInt(scrollpos) + 420;
    var scrollHeight = $('#chat-messages').prop('scrollHeight');

    $('#chat-messages').html(response);
    if (scrollpos < scrollHeight){

    } else{
        $('#chat-messages').scrollTop($('#chat-messages').prop('scrollHeight'));
    }

})

}

    $('form').submit(function () {
        //alert('form is submit jquery');
        var message = $('.textarea').val();

        if ($(".textarea").val()) {

        $.post('handlers/messages.php?action=sendMessage&message='+message, function (response) {
            //alert(response);
            if (response==1){
                LoadChat();
                document.getElementById('msgform').reset();
            }
        });

    } else {

        myFunction();

}
        return false;

    })
</script>

任何帮助都将受到高度赞赏

1 个答案:

答案 0 :(得分:0)

您在多个地方不带参数的情况下调用 ResultSet rs = null; try { Statement stmt = queryDb.createStatement(); rs = stmt.executeQuery(strQuery); } catch (SQLException e) { e.printStackTrace(); } while (rs.next()) { System.out.println(rs.getString("columnName")); } 。调用不带参数的函数会将其参数设置为LoadChat()(如果有)。如果要设置undefined,请将其作为函数调用的参数提供。

email