焦点事件无法以HTML格式运行

时间:2017-01-14 06:05:28

标签: javascript jquery html

我的HTML代码是

<body>
    <form name="contactus" id='contactus' action="test1.php" method="post" enctype="multipart/form-data">
        <input type="hidden" value="2" name="Tab" id="Tab">
        <div class="row underlinDv">
            <div class="col-sm-4">
                Your Email :
            </div>
            <div class="col-sm-8">
                <input maxlength="100" type="email" class="form-control" placeholder="Enter Email" name="UEmailLogin" id="UEmailLogin" minlength="3">
            </div>
        </div>
        <!--18-->
        <div class="row underlinDv">
            <div class="col-sm-8 col-md-offset-4">
                <input type="submit" id="demo2GetTags" class="btn btn-primary btn-md" value="Submit" onclick='search()' />
            </div>
        </div>
    </form>
    <script>
    function search() {
        var Tab = document.getElementById("Tab").value;
        alert(Tab);
        var Eamillogin = document.getElementById("UEmailLogin").value;
        if (Eamillogin == "") {
            alert("please enter email");
            Eamillogin.focus();
        }
    }
    </script>
</body>

当我点击提交按钮时,JavaScript提醒显示&#34;请输入电子邮件&#34;。它工作正常。但Eamillogin.focus();无效。显示警报后,表单自动指向test1.php页面.Pointer不关注电子邮件。如何纠正?

1 个答案:

答案 0 :(得分:1)

#UEmailLogin变量包含element.focus();元素的值。

要将焦点设置在元素上,请使用var Eamillogin = document.getElementById("UEmailLogin"); // Removed .value from here if (Eamillogin.value === "") { // Added .value here alert("please enter email"); Eamillogin.focus(); // Focus element }

这是更新后的代码

function search() {
  var Tab = document.getElementById("Tab").value;

  var Eamillogin = document.getElementById("UEmailLogin");
  if (Eamillogin.value === "") {
    alert("please enter email");
    Eamillogin.focus();
  }
}

<form name="contactus" id='contactus' action="test1.php" method="post" enctype="multipart/form-data">
  <input type="hidden" value="2" name="Tab" id="Tab">
  <div class="row underlinDv">
    <div class="col-sm-4">
      Your Email :
    </div>
    <div class="col-sm-8">
      <input maxlength="100" type="email" class="form-control" placeholder="Enter Email" name="UEmailLogin" id="UEmailLogin" minlength="3">
    </div>
  </div>
  <!--18-->
  <div class="row underlinDv">
    <div class="col-sm-8 col-md-offset-4">
      <input type="submit" id="demo2GetTags" class="btn btn-primary btn-md" value="Submit" onclick='search()' />
    </div>
  </div>
</form>
 @IBAction func map_button(_ sender: AnyObject) {
    let elephant = CLLocationCoordinate2DMake(-39.069150, 174.081657)
    let elephant_place = MKPlacemark(coordinate:elephant)
    let mapItem = MKMapItem(placemark:elephant_place)
    let options = [MKLaunchOptionsDirectionsModeKey:
        MKLaunchOptionsDirectionsModeDriving]

    mapItem.openInMaps(launchOptions: options)

}