简体   繁体   English

hide()函数在jQuery中不起作用

[英]hide() function doesn't work in jQuery

I am having problems with jQuery validation of a form. 我遇到了表单的jQuery验证问题。

This is my javascript file: test.js. 这是我的javascript文件:test.js.

$(function() {
$('.error').hide();
    $(".button").click(function() {
        $('.error').hide();
  var name = $("input#name").val();
    if (name == "") {
    $("label#name_error").show();
    $("input#name").focus();
    return false;
  }
});
});

This is my html file. 这是我的html文件。

<html>
<head>
<script type="text/javascript" src="test.js"></script>
<script type="text/javascript" src="jquery-1.5.2.min.js"></script>
</head>
<body>
<div id="contact_form">
<form name="contact" action="">
  <label for="name" id="name_label">Name</label>
  <input type="text" name="name" id="name" size="30" value="" class="text-input" />
  <label class="error" for="name" id="name_error">This field is required.</label>
</form> 
</div>
</body>
</html>

I placed these two files together with the jQuery file in the same directory on my server, but the content "This field is required." 我将这两个文件与jQuery文件一起放在我服务器上的同一目录中,但内容为“此字段是必需的”。 is still visible. 仍然可见。 It seems like hide() function in the test.js doesn't work. 看起来test.js中的hide()函数不起作用。 Can anyone point me out where is wrong? 谁能指出我哪里出错了? Great thanks! 万分谢意!

Put your jquery file before your test.js . 将您的jquery文件放在test.js之前。

<script type="text/javascript" src="jquery-1.5.2.min.js"></script>
<script type="text/javascript" src="test.js"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM