0

I am writing a Google Chrome Extension that needs to capture addresses in the "To" field of a new GMail e-mail. This is what I currently am working with (jQuery 2.0.2 is being used):

$('textarea[name="to"]').bind("enterKey",function(e){
        alert($('textarea[name="to"]').val()); // this is definitely the "To" field
});
$('textarea').keyup(function(e){
        if(e.keyCode == 13) {
        $(this).trigger("enterKey");
}
});

Each time I press Enter in the To field with the code above, an empty alert() box fires. However, if I change the alert() to display an arbitrary value, like alert('david'); the message david is inside the alert box.

My question is, why is there an empty string coming off of the "To" field's .val() when I press Enter?

Thanks!

4

1 回答 1

4

Gmail 的 to Field 不像 textarea 那样工作,它从文本中获取电子邮件并在文本区域之前为每封电子邮件创建一个 div。

您应该检查 DOM 以查看那里发生了什么。

这是代码的相关部分。

<div class="vR">
  <span class="vN vQ" email="email1@example.com">
    <div class="vT">email1</div>
    <div class="vM"></div>
  </span>
  <input name="to" type="hidden" value="email1">
</div>
<div class="vR">
  <span class="vN vQ" email="email2@example.com">
    <div class="vT">email2</div>
    <div class="vM"></div>
  </span>
  <input name="to" type="hidden" value="email2">
</div>
<textarea rows="1" id=":2y" class="vO" name="to" spellcheck="false" autocomplete="false" tabindex="1" dir="ltr" aria-haspopup="true" style="width: 950px;"></textarea>

所以你可以收到这样的电子邮件,

$('textarea[name="to"]').bind("enterKey", function(e){
    var emails = [];
    $(this).siblings("div.vR").find("span.vN").each(function() {
      emails.push($(this).attr("email"));
    })
    alert(emails.join(", "));
});
于 2013-07-11T02:53:16.357 回答