【问题标题】:jQuery Validation Plugin - can't chain function after successful validation [duplicate]jQuery Validation Plugin - 成功验证后无法链接函数[重复]
【发布时间】: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();
            });


        }
    });
}
});

});

【问题讨论】:

  • 如果您自己解决了问题,请将解决方案发布在答案中,而不是问题中。

标签: javascript jquery forms jquery-validate


【解决方案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();
            });


        }
    });
}
});

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多