简体   繁体   English

jQuery表单验证和语法

[英]jQuery Form Validation and Syntax

I am trying to calculate this but it is not working. 我正在尝试计算这个,但是它不起作用。 I also tried the debugger and it says i have a syntax error on this line $("#button").click(function() { 我还尝试了调试器,它说我在此行$("#button").click(function() {

Here is my full jQuery code: 这是我完整的jQuery代码:

 $(document).ready(function(){
  $("#form").validate({
            rules: {
                'wind': {
                    required: true,
                    positiveNumber:true  
                }
            },
            messages: {
                'wind': {
                    required: "This value is required",
                    positiveNumber:"Positive numbers only please"
                }
            }


$("#button").click(function() {
    var temp = Number($('input[name=temp]').val());
    var wind = Number($('input[name=wind]').val());
    var result = (temp - (1.5 * wind));
    $('#result').text(result + " Degree Celsius");

    });
});

});

html html

            <form style="text-align:center;font-size:120%;" id="form">
    <label>
        <strong>Temperature</strong>
        <input name="temp" type="text" value="" id="temp">
    </label>
    <label>
        <strong>Wind Speed </strong>
        <input name="wind" type="text" value="" id="wind">
    </label>
    <br>
    <br>
    <button value="Submit" id="button" class="button">Submit</button>
    <br>
    <br>
    <button type="reset" value="Reset" id="button1">Reset</button>
</form>

</div>
<div class="res">
    <div id="result"></div>
</div>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="jquery-1.11.2.js">
</script>
<script type="text/javascript" src="script2.js"></script>

Piece of cake, passed your code through JSBeautifier (its just a pretty-formatter tool), and showed me this: 小菜一碟,通过JSBeautifier (它只是一个漂亮的格式化工具)传递了代码,并向我展示了这一点:

$(document).ready(function() {
     $("#form").validate({
         rules: {
             'wind': {
                 required: true,
                 positiveNumber: true
             }
         },
         messages: {
             'wind': {
                 required: "This value is required",
                 positiveNumber: "Positive numbers only please"
             }
         } 
         // NOTICE NOTICE NOTICE
         $("#button").click(function() {
             var temp = Number($('input[name=temp]').val());
             var wind = Number($('input[name=wind]').val());
             var result = (temp - (1.5 * wind));
             $('#result').text(result + " Degree Celsius");
         });
     }); // this guy should be where it screams notice
 });

The sample below shouldn't have syntax errors. 以下示例不应有语法错误。

$(document).ready(function() {
    $("#form").validate({
        rules: {
            'wind': {
                required: true,
                positiveNumber: true
            }
        },
        messages: {
            'wind': {
                required: "This value is required",
                positiveNumber: "Positive numbers only please"
            }
        }
    });

    $("#button").click(function() {
        var temp = Number($('input[name=temp]').val());
        var wind = Number($('input[name=wind]').val());
        var result = (temp - (1.5 * wind));
        $('#result').text(result + " Degree Celsius");
    });
});

I don't think that $("#button") should be inside $("#form").validate() . 我不认为$("#button")应该在$("#form").validate() Take it outside validate function but keep it inside $(document).ready(function() 将其置于validate函数之外,但将其保留在$(document).ready(function()

EDIT 编辑

Looking at your HTML now... In imports I dont see jquery.validate.min.js .. how will it work? 现在查看您的HTML ...在导入中,我看不到jquery.validate.min.js ..它将如何工作?

Refer this question for more insights 请参阅此问题以获取更多见解

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

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