onfocus无法在javascript中工作

时间:2015-12-29 08:27:22

标签: javascript jquery html

我正在整理一些javascript表单验证,我无法弄清楚如何让这个onfocus抓住我的div ...我只需要它提醒我我的onfocus功能正在接触我的姓氏imput。 .. :(

HTML:

<div id="contact">
    <form name="form1" action="send.php" method="post" id="contact_f">
      <table id="contact_table">
        <tr>
          <td class="col1" colspan="2">
            <h1 class="center_text">Contact Aron</h1>
            <div id="error_messages">
              errors
            </div>
          </td>
        </tr>
        <tr>
          <td class="col1">
            <label for="first_name">First Name*</label><br>
            <input id="first_name" required name="Name" type="text" pattern="[a-zA-Z]+">
          </td>
          <td class="col2">
            <label for="last_name">Last Name*</label><br>
            <input id="last_name" required type="text">
          </td>
        </tr>
        <tr>
          <td class="col1">
            <label for="email">Email*</label><br>
            <input id="email" required type="email">
          </td>
          <td class="col2">
            <label for="confirm_email">Confirm Email*</label><br>
            <input id="confirm_email" required type="text">
          </td>
        </tr>
        <tr>
          <td class="col1">
            <label for="phone">Phone Number   <span id="color_gray">xxx-xxx-xxxx</span></label><br>
            <input id="phone" type="tel" pattern="\d{3}[\-]\d{3}[\-]\d{4}">
          </td>
          <td class="col2">

          </td>
        </tr>
        <tr class="col2">
          <td class="col1" colspan="2">
            <label for="message">Message*</label><br>
            <textarea id="message" required type="text"></textarea>
          </td>
        </tr>
        <tr>
          <td class="col1" colspan="2">
            <button id="submit_button" type="submit" value="submit">Submit Form</button>
          </td>
        </tr>
      </table>
  </form>

有问题的javascript:

    document.getElementById("last_name").onfocus=function() {
      alert("last name");
    };

3 个答案:

答案 0 :(得分:0)

您需要使用:

document.getElementById("last_name").onfocus=function() {
   alert(this.value);
};

答案 1 :(得分:0)

实际上你的代码工作正常。您可以使用jquery尝试它,就像这样 -

$(document).ready(function() {
$( "#last_name" ).focus(function() {
alert("Last name");
});  
});

答案 2 :(得分:0)

试试这个

this.unique = input