【问题标题】:Uncaught ReferenceError: onPrevious is not defined at HTMLAnchorElement.onclick未捕获的 ReferenceError:onPrevious 未在 HTMLAnchorElement.onclick 中定义
【发布时间】:2018-07-12 11:37:41
【问题描述】:

我遇到了 Uncaught ReferenceError: previous is not defined at HTMLAnchorElement.onclick 请帮助我,我已经尝试过之前针对同一问题提供的所有解决方案,但没有帮助。 我已经有了 jquery 版本和 bootstrap 版本。 我正在粘贴整个脚本代码和放置按钮的 html 标记。

<ul class="pager wizard">
   <li class="previous"><a  href="javascript:;" >Previous</a></li>
   <li class="next"><a href="javascript: void(0);" onclick="onPrevious(tab, navigation, index);">Next</a></li>
</ul>


<script>
$(document).ready(function() {
    // You don't need to care about this function
    // It is for the specific demo
    function adjustIframeHeight() {
        var $body   = $('body'),
                $iframe = $body.data('iframe.fv');
        if ($iframe) {
            // Adjust the height of iframe
            $iframe.height($body.height());
        }
    }

    $('#installationForm')
        .formValidation({
            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            // This option will not ignore invisible fields which belong to inactive panels
            excluded: ':disabled',
            fields: {
                type_of_service: {
                    validators: {
                        notEmpty: {
                            message: 'The type of service is required'
                        }
                    }
                },
                academic_level: {
                    validators: {
                        notEmpty: {
                            message: 'The academic level is required'
                        }
                    }
                },

                type_of_paper: {
                    validators: {
                        notEmpty: {
                            message: 'The type of paper is required'
                        }
                    }
                },

                topic: {
                    validators: {
                        notEmpty: {
                            message: 'The topic is required'
                        }
                    }
                },

                subject: {
                    validators: {
                        notEmpty: {
                            message: 'The subject is required'
                        }
                    }
                },

                type_of_citation: {
                    validators: {
                        notEmpty: {
                            message: 'The type of citation is required'
                        }
                    }
                },

               order_deadline: {
                    validators: {
                        notEmpty: {
                            message: 'The order deadline is required'
                        }
                    }
                },
                pages: {
                    validators: {
                        notEmpty: {
                            message: 'The number of pages is required'
                        }
                    }
                },
                type_of_spacing: {
                    validators: {
                        notEmpty: {
                            message: 'The type of spacing is required'
                        }
                    }
                },

                category_of_writer: {
                    validators: {
                        notEmpty: {
                            message: 'Select your prefered writer'
                        }
                    }
                },

                fullname: {
                    validators: {
                        notEmpty: {
                            message: 'Please specify your full name'
                        }
                    }
                },

                email: {
                    validators: {
                        notEmpty: {
                            message: 'The email address is required'
                        },
                        emailAddress: {
                            message: 'The email address is not valid'
                        }
                    }
                },
                dbServer: {
                    validators: {
                        notEmpty: {
                            message: 'The server IP is required'
                        },
                        ip: {
                            message: 'The server IP is not valid'
                        }
                    }
                },
                dbName: {
                    validators: {
                        notEmpty: {
                            message: 'The database name is required'
                        }
                    }
                },
                dbUser: {
                    validators: {
                        notEmpty: {
                            message: 'The database user is required'
                        }
                    }
                }
            }
        })
        .bootstrapWizard({
            tabClass: 'nav nav-pills',
            onTabClick: function(tab, navigation, index) {
                return validateTab(index);
            },
            onNext: function(tab, navigation, index) {
                var numTabs    = $('#installationForm').find('.tab-pane').length,
                    isValidTab = validateTab(index - 1);
                if (!isValidTab) {
                    return false;
                }

                if (index === numTabs) {
                    // We are at the last tab

                    // Uncomment the following line to submit the form using the defaultSubmit() method
                     $('#installationForm').formValidation('defaultSubmit');

                    // For testing purpose
                    //$('#completeModal').modal();
                }

                return true;
            },
            onPrevious: function(tab, navigation, index) {
                return validateTab(index + 1);
            },
            onTabShow: function(tab, navigation, index) {
                // Update the label of Next button when we are at the last tab
                var numTabs = $('#installationForm').find('.tab-pane').length;
                $('#installationForm')
                    .find('.next')
                        .removeClass('disabled')    // Enable the Next button
                        .find('a')
                        .html(index === numTabs - 1 ? 'Place Order' : 'Next');

                // You don't need to care about it
                // It is for the specific demo
                adjustIframeHeight();
            }
        });
    function validateTab(index) {
        var fv   = $('#installationForm').data('formValidation'), // FormValidation instance
            // The current tab
            $tab = $('#installationForm').find('.tab-pane').eq(index);

        // Validate the container
        fv.validateContainer($tab);
        var isValidStep = fv.isValidContainer($tab);
        if (isValidStep === false || isValidStep === null) {
            // Do not jump to the target tab
            return false;
        }
        return true;`enter code here`
    }

});`enter code here`
</script>

【问题讨论】:

  • onclick="onPrevious onPrevious 函数(这就是错误消息告诉您的内容)。你不能随便给一个这样的函数名,它必须是可访问的。
  • 我该如何解决?

标签: javascript jquery html


【解决方案1】:

一个简单的解决方案是在全局范围内声明它并在 jQuery 范围内重用它。

<ul class="pager wizard">
   <li class="previous"><a  href="javascript:;" >Previous</a></li>
   <li class="next"><a href="javascript: void(0);" onclick="onPrevious(tab, navigation, index);">Next</a></li>
</ul>


<script>

// Your function is declared globally.
function onPrevious(tab, navigation, index) {
    return validateTab(index + 1);
}

$(document).ready(function() {
    // You don't need to care about this function
    // It is for the specific demo
    function adjustIframeHeight() {
        var $body   = $('body'),
                $iframe = $body.data('iframe.fv');
        if ($iframe) {
            // Adjust the height of iframe
            $iframe.height($body.height());
        }
    }

    $('#installationForm')
        .formValidation({
            framework: 'bootstrap',
            icon: {
                valid: 'glyphicon glyphicon-ok',
                invalid: 'glyphicon glyphicon-remove',
                validating: 'glyphicon glyphicon-refresh'
            },
            // This option will not ignore invisible fields which belong to inactive panels
            excluded: ':disabled',
            fields: {
                type_of_service: {
                    validators: {
                        notEmpty: {
                            message: 'The type of service is required'
                        }
                    }
                },
                academic_level: {
                    validators: {
                        notEmpty: {
                            message: 'The academic level is required'
                        }
                    }
                },

                type_of_paper: {
                    validators: {
                        notEmpty: {
                            message: 'The type of paper is required'
                        }
                    }
                },

                topic: {
                    validators: {
                        notEmpty: {
                            message: 'The topic is required'
                        }
                    }
                },

                subject: {
                    validators: {
                        notEmpty: {
                            message: 'The subject is required'
                        }
                    }
                },

                type_of_citation: {
                    validators: {
                        notEmpty: {
                            message: 'The type of citation is required'
                        }
                    }
                },

               order_deadline: {
                    validators: {
                        notEmpty: {
                            message: 'The order deadline is required'
                        }
                    }
                },
                pages: {
                    validators: {
                        notEmpty: {
                            message: 'The number of pages is required'
                        }
                    }
                },
                type_of_spacing: {
                    validators: {
                        notEmpty: {
                            message: 'The type of spacing is required'
                        }
                    }
                },

                category_of_writer: {
                    validators: {
                        notEmpty: {
                            message: 'Select your prefered writer'
                        }
                    }
                },

                fullname: {
                    validators: {
                        notEmpty: {
                            message: 'Please specify your full name'
                        }
                    }
                },

                email: {
                    validators: {
                        notEmpty: {
                            message: 'The email address is required'
                        },
                        emailAddress: {
                            message: 'The email address is not valid'
                        }
                    }
                },
                dbServer: {
                    validators: {
                        notEmpty: {
                            message: 'The server IP is required'
                        },
                        ip: {
                            message: 'The server IP is not valid'
                        }
                    }
                },
                dbName: {
                    validators: {
                        notEmpty: {
                            message: 'The database name is required'
                        }
                    }
                },
                dbUser: {
                    validators: {
                        notEmpty: {
                            message: 'The database user is required'
                        }
                    }
                }
            }
        })
        .bootstrapWizard({
            tabClass: 'nav nav-pills',
            onTabClick: function(tab, navigation, index) {
                return validateTab(index);
            },
            onNext: function(tab, navigation, index) {
                var numTabs    = $('#installationForm').find('.tab-pane').length,
                    isValidTab = validateTab(index - 1);
                if (!isValidTab) {
                    return false;
                }

                if (index === numTabs) {
                    // We are at the last tab

                    // Uncomment the following line to submit the form using the defaultSubmit() method
                     $('#installationForm').formValidation('defaultSubmit');

                    // For testing purpose
                    //$('#completeModal').modal();
                }

                return true;
            },
            onPrevious: onPrevious, // You reuse it here.
            onTabShow: function(tab, navigation, index) {
                // Update the label of Next button when we are at the last tab
                var numTabs = $('#installationForm').find('.tab-pane').length;
                $('#installationForm')
                    .find('.next')
                        .removeClass('disabled')    // Enable the Next button
                        .find('a')
                        .html(index === numTabs - 1 ? 'Place Order' : 'Next');

                // You don't need to care about it
                // It is for the specific demo
                adjustIframeHeight();
            }
        });
    function validateTab(index) {
        var fv   = $('#installationForm').data('formValidation'), // FormValidation instance
            // The current tab
            $tab = $('#installationForm').find('.tab-pane').eq(index);

        // Validate the container
        fv.validateContainer($tab);
        var isValidStep = fv.isValidContainer($tab);
        if (isValidStep === false || isValidStep === null) {
            // Do not jump to the target tab
            return false;
        }
        return true;
    }

});
</script>

编辑

这里有一个 sn-p 来证明它是有效的。

<ul class="pager wizard">
   <li class="previous"><a  href="javascript:;" >Previous</a></li>
   <li class="next"><a href="javascript: void(0);" onclick="onPrevious('tab', 'navigation', 'index');">Next</a></li>
</ul>

<script>
// Your function is declared globally.
function onPrevious(tab, navigation, index) {
    console.log(tab, navigation, index)
    //return validateTab(index + 1);
}
</script>

【讨论】:

  • 谢谢你修复了 2 个错误,但我仍然收到 Uncaught ReferenceError: onPrevious is not defined at HTMLAnchorElement.onclick ((index):753) onclick @ (index):753
  • 这行得通。根据您发布的内容,我添加了一个 sn-p 作为证明。我没有你的行号,所以它无济于事。你确定你做了修改吗?
  • 我真的很感谢它解决了我剩下的所有错误但是当我点击下一个按钮时它会出现
  • 我确定我做了修改
猜你喜欢
  • 2019-11-08
  • 1970-01-01
  • 2018-04-14
  • 2020-09-20
  • 1970-01-01
  • 2020-08-28
  • 2021-09-09
  • 2017-08-04
  • 2021-06-20
相关资源
最近更新 更多