我的jquery总是只给我第一张唱片

时间:2013-10-31 10:09:59

标签: javascript jquery arrays

我在ajax工作,我从服务器获取记录,然后过滤该记录并尝试通过类EMP_EMAIL表单获取所有电子邮件,但它只给我第一个。我正在做的是在一个函数中从服务器获取记录并在对话框中显示它。因为我得到的记录是那些护照将在7天后到期的员工的记录。所以我需要发送电子邮件,为此我在对话框中的每个员工数据下面放了一个按钮。现在为此目的,我需要每个员工的电子邮件及其各自的按钮,以便将电子邮件发送给合适的人。所以我正在做的是在一个功能中获取记录并使用另一个功能用于电子邮件,为此我需要来自第一个功能的所有员工电子邮件并将其传递给另一个功能但问题是我无法获得所有相应的电子邮件但是第一。 因为我对class =“EMP_EMAIL”的用户一次又一次地重复相同的结构。

下面是代码

   var employee_email='';
   var flag = false;

   function showCustomer()
   {
  // fire off the request to ajax_stufflist.php
  request = $.ajax({
    url: "ajax_stufflist.php?"+url,
    type: "post",
    success: function(data){
        if(data != ''){
    var response = $(data).find("#gmp_stuff").html();
    employee_email=$(response).find(".EMP_EMAIL>span").html();
    //alert(employee_email);
    $("#user_responses").html(response);

            $("#user_responses").dialog({
                dialogClass:'transparent',
                resizable: false,
                draggable: false,
                modal: true,

                width: 1000,
                autoOpen: false,
                overlay: { opacity: 0 }
            });

            $('#user_responses').dialog('open');
            $('#user_responses').css('display','');
            flag = true;
        }
    },
    error:function(){
        alert("failure");
        $("#user_responses").html('error occured');
      }
  });

}

function sendEmail(){
   if(flag)
   {
    request = $.ajax({
        url: "send_email.php?"+employee_email,
        type: "post",
        success: function(data){
            $("#email_responses").html();
        },
        error:function(){
            alert("failure");
            $("#email_responses").html('error occured');
        }
    });
}
else
    alert('Sending email is unavailable currently. PLease try after some time.');
}

这是输入按钮。

 <div class="send_email_notification">
        <input type="submit" value="Send Email" onClick="sendEmail();">
    </div>

我尽力详细阐述我的问题,但我不知道我是否成功但是在底线我必须收到所有用户的电子邮件,这些用户在ajax之后返回,然后点击发送电子邮件按钮时应该发送发送给该特定用户的电子邮件。

2 个答案:

答案 0 :(得分:1)

jQuery函数充当getter(.val().html()等,没有参数)通常只作用于第一个匹配元素。如果你想获得所有这些值的值,你需要迭代。有几种方法可以做到这一点,具体取决于你之后想要用它们做什么。

第一个使用.each()

$(response).find(".EMP_EMAIL>span").each(function(i, span) {
    var employee_email = $(span).html(); // can also use this instead of span
    // use employee_email for the current iteration here, it's not visible outside
});

第二个使用.map()返回所有电子邮件地址的数组:

var employee_emails = $(response).find(".EMP_EMAIL>span").map(function(i, span) {
    return $(span).html(); // again, could also use this instead of span
}).get();

答案 1 :(得分:0)

I reviewed your code, The following suggestion may help:

Make sure the showCustomer() function execute before sendEmail() function.      

Eg.

 function sendEmail(){
   showCustomer(); 
   if(flag){....}
 }    


If your want to take email address from browser use .text() instead of .html()

Eg.      

  employee_email = $(response).find(".EMP_EMAIL>span").text();