嘗試驗證可見字段時出現typeError undefined is not a function
錯誤。重用jQuery從匿名函數驗證
我定義了一個變量,該變量將全局範圍內的jquery驗證程序保留,但會執行。 從由symfony/twig/JqueryValidationBundle動態生成的匿名函數閉包中驗證()。
我這樣做是因爲我想將生成的代碼用於包含在單獨的js文件中的自定義函數。
可以做到這一點嗎?還是有另一種重用生成的代碼的方式?
這是生成的函數:
<script>
var form, groups;
(function($) {
form = $("form[name=\"scs_intakebundle_intake\"]");
groups = {"Default": false,"firstPanel": false};
form.find("*[name=\"scs_intakebundle_intake\x5Bsubmit\x5D\"]").click(function() {
groups = {"Default": true,"firstPanel": true};
});
form.validate({
rules: {
"scs_intakebundle_intake\x5BfirstName\x5D": {
"required": {depends: function() {return groups["firstPanel"]; }}
},
"scs_intakebundle_intake\x5BlastName\x5D": {
"required": {depends: function() {return groups["firstPanel"]; }}
}
}});
})(jQuery);
</script>
這是intake.js
function nextPanel() {
var result = true;
$('input:visible').each(function(i,item){
//form is the global var that .validate() was already run.
//below is where form.element is undefined.
result = form.element(item) && result;
//the next line did not work either, but I though I'd share
result = item.valid() && result;
});
if (result) {
$('.first').slideUp();
$('.second').slideDown();
}
}
此功能是HTML表單:
<form name="scs_intakebundle_intake" method="post" action="/v2/web/app_dev.php/" novalidate="novalidate">
<input type="hidden" id="scs_intakebundle_intake__token" name="scs_intakebundle_intake[_token]" value="956WbsNijk_3F_8X_0IGolrcdZaZzar93OwVHAxspyo">
<div class="first">
<div class="row">
<div class="medium-7 columns">
<input type="text" id="scs_intakebundle_intake_firstName" name="scs_intakebundle_intake[firstName]" required="required" maxlength="255" aria-required="true">
</div>
</div>
<div class="row">
<div class="medium-7 columns">
<input type="text" id="scs_intakebundle_intake_lastName" name="scs_intakebundle_intake[lastName]" required="required" maxlength="255" aria-required="true">
</div>
</div>
<div>
<button type="button" id="scs_intakebundle_intake_next1" name="scs_intakebundle_intake[next1]" style="margin:0;" class="next1 panelButton" onclick="nextPanel();">Find my plan</button>
</div>
</div>
<div class="second">
<div class="row">
<div class="medium-7 columns">
<div id="scs_intakebundle_intake_married">
<input type="radio" id="scs_intakebundle_intake_married_0" name="scs_intakebundle_intake[married]" value="0">
<label for="scs_intakebundle_intake_married_0">No</label><input type="radio" id="scs_intakebundle_intake_married_1" name="scs_intakebundle_intake[married]" value="1">
<label for="scs_intakebundle_intake_married_1">Yes</label></div>
</div>
</div>
<div class="row" style="padding: 1.1rem">
<button type="button" id="scs_intakebundle_intake_prev1" name="scs_intakebundle_intake[prev1]" style="float:left" class="prev1 panelButton" onclick="previousPanel();">Previous</button>
<button type="submit" id="scs_intakebundle_intake_submit" name="scs_intakebundle_intake[submit]" class="panelButton">Find my plan</button>
</div>
</div>
</form>
我沒有驗證鉻devtools控制檯,那$('input:visible')
確實包含我想檢查的表單元素。 form.validate()
確實會返回一個$ .validator對象。
驗證在單擊最終提交按鈕時不起作用,但當我嘗試觸發自定義函數的驗證時不起作用。
任何想法?
你是什麼意思的「重用」...使用相同的調用多個表單上的'.validate()'或多次調用'.validate()'在同一個表單上? – Sparky 2014-12-05 02:03:46