获取Jquery错误:SyntaxError:missing:在表单验证中的属性id之后

时间:2014-12-19 10:38:30

标签: jquery

以下是我的联系表格代码,但是当我试图仅在名称上测试时出现错误

<script>
    $(document).ready(function(){

      $("#submit").click(function({

        var name = $("#name").val();
        if (name =='')
      {
          alert("Please Enter Your name");
      }

        });               
    )};
</script>
<body>
<div id="mainform">
<h2></h2>
<!-- Required Div Starts Here -->
<form id="form">
<h3></h3>
<p id="returnmessage"></p>
<label>Name: <span>*</span></label>
<input type="text" id="name" placeholder="Name"/>
<label>Email: <span>*</span></label>
<input type="text" id="email" placeholder="Email"/>
<label>Contact No: <span>*</span></label>
<input type="text" id="contact" placeholder="10 digit Mobile no."/>
<label>Message:</label>
<textarea id="message" placeholder="Message......."></textarea>
<input type="button" id="submit" value="Send Message"/>
</form>
</div>
</body>
</html>

运行此代码后,在控制台中出现以下错误。谷歌搜索到处都找不到解决方案。请帮助我。

SyntaxError: missing : after property id
    var name = $("#name").val();

3 个答案:

答案 0 :(得分:2)

您的括号和大括号在#submit点击功能上不正确:

$(document).ready(function(){
    $("#submit").click(function() { // note extra ) here
        // your code...
    });               
}); // swapped order of braces here

答案 1 :(得分:0)

只需更正$(document).ready(function(){

的右括号
<script>
    $(document).ready(function(){
      $("#submit").click(function(){  // <---------- correct here also
        var name = $("#name").val();
        if (name =='')
        {
          alert("Please Enter Your name");
        }
      });               
    }); // <---------
</script>

答案 2 :(得分:0)

您的代码将是这样的:

<script>
        $(document).ready(function () {
            $("#submit").click(function () {
                var name = $("#name").val();
                if (name == '')
                {
                    alert("Please Enter Your name");
                }
            });
        });
    </script>