jQuery Validation Plugin - 成功验证后无法链接函数[重复]

Posted

技术标签:

【中文标题】jQuery Validation Plugin - 成功验证后无法链接函数[重复]【英文标题】:jQuery Validation Plugin - can't chain function after successful validation [duplicate] 【发布时间】:2018-01-23 20:09:42 【问题描述】:

所以我想做的就是这部分代码:

$(document).ready(function ()  //predaj formu i automatski crtaj graf
    $('#myform').on('submit', function (e) 
      e.preventDefault();

      $.ajax(
        type: 'post',
        url: 'upit.php',
        data: $('#myform').serialize(),
        success: function () 
            var dataPoints = [];
            $.getJSON("rez.json", function(data)  //uzmi JSON za tocke grafa
                $.each(data, function(key, value)
                    dataPoints.push(x: value[0], y: parseInt(value[1]));
                );

                var chart = new CanvasJS.Chart("chartContainer",
                    zoomEnabled: true,
                    animationEnabled: false,
                    axisY: 
                        title: "Power received"
                    ,
                    axisX: 
                        title: "Distance"
                    ,
                    data: [
                        type: "line",
                        dataPoints : dataPoints,
                    ]
                );
                chart.render();
            );

            $.ajax( //vrati rezultat
                url:"novi.json",
                success:function(result)
                    $("#disabledInput").val(result);
                
            );        
        
    );
);
);

执行

通过这部分代码验证成功提交后:

$(document).ready(function () 

$('#myform').validate( // initialize the plugin
    rules: 
        n1: 
            required: true,
            email: true
        ,
        n2: 
            required: true,
            minlength: 5
        
    ,
    errorPlacement: function(error, element) 
        error.appendTo('#nameError');
    ,

    submitHandler: function (form)  // for demo
        alert('valid form submitted'); // for demo
        return false; // for demo
    
);

);

我只是无法正确链接它,我知道这两个是分开的脚本,所以我需要它们以某种方式链接。谢谢!

此外,我需要将 ajax 提交(第一个代码)代码分开,因为我想用许多其他函数调用它(播放是添加一个滑块,所以我在每次更改滑块等时提交它......)但我'不知道如何

编辑:我做到了,这是答案

$(document).ready(function () 

$('#myform').validate( // initialize the plugin
rules: 
    n1: 
        required: true,

    ,
    n2: 
        required: true,

    
,
errorPlacement: function(error, element) 
    error.appendTo('#nameError');
,

submitHandler: function (form) 


      $.ajax(
        type: 'post',
        url: 'upit.php',
        data: $('#myform').serialize(),
        success: function()
            var dataPoints = [];
            $.getJSON("rez.json", function(data)  //uzmi JSON za tocke 
grafa
                $.each(data, function(key, value)
                    dataPoints.push(x: value[0], y: parseInt(value[1]));
                );

                var chart = new CanvasJS.Chart("chartContainer",
                    zoomEnabled: true,
                    animationEnabled: false,
                    axisY: 
                        title: "Power received"
                    ,
                    axisX: 
                        title: "Distance"
                    ,
                    data: [
                        type: "line",
                        dataPoints : dataPoints,
                    ]
                );
                chart.render();
            );


        
    );

);

);

【问题讨论】:

如果您自己解决了问题,请将解决方案发布在答案中,而不是问题中。 【参考方案1】:

我做到了,这是答案

$(document).ready(function () 

$('#myform').validate( // initialize the plugin
rules: 
    n1: 
        required: true,

    ,
    n2: 
        required: true,

    
,
errorPlacement: function(error, element) 
    error.appendTo('#nameError');
,

submitHandler: function (form) 


      $.ajax(
        type: 'post',
        url: 'upit.php',
        data: $('#myform').serialize(),
        success: function()
            var dataPoints = [];
            $.getJSON("rez.json", function(data)  //uzmi JSON za tocke 
grafa
                $.each(data, function(key, value)
                    dataPoints.push(x: value[0], y: parseInt(value[1]));
                );

                var chart = new CanvasJS.Chart("chartContainer",
                    zoomEnabled: true,
                    animationEnabled: false,
                    axisY: 
                        title: "Power received"
                    ,
                    axisX: 
                        title: "Distance"
                    ,
                    data: [
                        type: "line",
                        dataPoints : dataPoints,
                    ]
                );
                chart.render();
            );


        
    );

);

);

【讨论】:

以上是关于jQuery Validation Plugin - 成功验证后无法链接函数[重复]的主要内容,如果未能解决你的问题,请参考以下文章

jQuery - 学习笔记 - jQuery Validation Plugin

jQuery Validation Plugin - 简单的实现

jQuery Validation Plugin - 成功验证后无法链接函数[重复]

jQuery Validation Plugin:如何测试有效且仅测试有效

JQuery Validation Plugin - 如果用户选择任一单选按钮,则验证字段。

MVC model 验证失效 Unexpected token u in JSON at position 0jquery-plugin-validation