找到你要的答案

Q:Onchange is uncaught mix of javascript and jquery code

Q:Onchange是捕获JavaScript和jQuery代码组合

The code is supposed to based on the select option do checkseason() function in javascript and adjust accordingly or do nothing (for now). The error that I get right now is Uncaught ReferenceError: checkseason is not defined on select value="checkseason();" line

   echo '<form id="first_form" action="#" method="POST" style="border: 0; margin: 0;">';
                        echo '<input type="hidden" value="'.$formid.'" name="formid">';
                        echo '<h1>Season, Patch version & amount of champions</h1>';
                        echo '<select id="season" onchange="checkseason();">';
                            echo '<option value="newseasons" selected>Season 3+</option>';
                            echo '<option value="oldseasons">Season 1, 2</option>';
                        echo '</select>';
                        echo '<input id="patch" type="text" name="patch" placeholder="e.g. 4.20" required autofocus><br/>';
                        echo '<input placeholder="Number of Champions" value="1" type="number" name="champ_number" min="1" max="20" required><br/>';
                        echo '<input type="submit" value="next">';
                    echo '</form>'; 

And Javascript/Jquery part of the code

$(document).ready(function(){
                function checkseason(){
                    d = document.getElementById("season").value;
                        if(d==='newseasons'){
                            $('#first_form').validate({
                                rules: {
                                    patch: {
                                        required: true,
                                        minlength: 4,
                                        remote: {
                                            url: "../checkpatch.php",
                                            type: "post"
                                            }
                                        }
                                    },
                                messages: {
                                    patch: {
                                        required: "Please enter patch version.",
                                        remote: "Patch with this number already exists.",
                                        minlength: "Please enter 3 numbers"
                                        }
                                    },

                            });
                        $.mask.definitions['~'] = "[+-]";
                        $.mask.definitions['x'] = '[0-9]';
                        $.mask.definitions['*'] = '[-A-Za-z0-9._\\\/?&=~]';

                        $("#patch").mask("x.xx", {
                            placeholder: ""
                        });

                    }
                    else if(d==='oldseasons'){}
                }
     });

代码应该是基于选择的选择做checkseason()函数在JavaScript和相应调整或什么也不做,(现在)。错误,我现在是未捕获的referenceerror:checkseason不是选择值=“checkseason()定义;”

   echo '<form id="first_form" action="#" method="POST" style="border: 0; margin: 0;">';
                        echo '<input type="hidden" value="'.$formid.'" name="formid">';
                        echo '<h1>Season, Patch version & amount of champions</h1>';
                        echo '<select id="season" onchange="checkseason();">';
                            echo '<option value="newseasons" selected>Season 3+</option>';
                            echo '<option value="oldseasons">Season 1, 2</option>';
                        echo '</select>';
                        echo '<input id="patch" type="text" name="patch" placeholder="e.g. 4.20" required autofocus><br/>';
                        echo '<input placeholder="Number of Champions" value="1" type="number" name="champ_number" min="1" max="20" required><br/>';
                        echo '<input type="submit" value="next">';
                    echo '</form>'; 

和代码的JavaScript / jQuery的一部分

$(document).ready(function(){
                function checkseason(){
                    d = document.getElementById("season").value;
                        if(d==='newseasons'){
                            $('#first_form').validate({
                                rules: {
                                    patch: {
                                        required: true,
                                        minlength: 4,
                                        remote: {
                                            url: "../checkpatch.php",
                                            type: "post"
                                            }
                                        }
                                    },
                                messages: {
                                    patch: {
                                        required: "Please enter patch version.",
                                        remote: "Patch with this number already exists.",
                                        minlength: "Please enter 3 numbers"
                                        }
                                    },

                            });
                        $.mask.definitions['~'] = "[+-]";
                        $.mask.definitions['x'] = '[0-9]';
                        $.mask.definitions['*'] = '[-A-Za-z0-9._\\\/?&=~]';

                        $("#patch").mask("x.xx", {
                            placeholder: ""
                        });

                    }
                    else if(d==='oldseasons'){}
                }
     });
answer1: 回答1:

You need to remove the $(document).ready(function(){}); part so that the function is loaded before the page is loaded.

你需要删除$(document)。准备(function() { });部分这样的功能是在页面加载时加载。

javascript  jquery