我在WordPress驅動的站點上創建了一個表單。目前它正在使用RequireJS爲頁面元素添加兩個按鈕,但我還希望需要控制jQuery Validation插件中的一些內容。按鈕的添加工作正常,但可悲的是,驗證不起作用。讓jQuery驗證插件與requirejs一起工作
的形式看起來像這樣的HTML:
<form id="contact" method="get" action="">
<fieldset id="contact-form-fields">
<p>
<label for="form_name" class="form-titles">Name</label>
<input name="form_name" id="form_name" type="text" placeholder="Name" aria-required="true" class="required">
</p>
<p>
<label for="form_email" class="form-titles">Email</label>
<input name="form_email" id="form_email" type="text" placeholder="Email" aria-required="true" value="">
</p>
<p>
<label for="msg_text" class="form-titles">Email</label>
<textarea tabindex="-8" rows="10" placeholder="Type your awesome message here." class="placeholder" name="msg_text" id="msg_text" aria-required="true" class="required"></textarea>
</p>
</fieldset>
<input type="submit" value="Send Your Message" name="submit" class="button">
</form>
RequireJS被附加到頁面這樣的:
<script data-main="http://livetest.kaidez.com/wp-content/themes/kaidez-2012/js/config.js" src="http://livetest.kaidez.com/wp-content/themes/kaidez-2012/js/require-jquery.js"></script>
上述config.js文件看起來像這樣:
require.config({
baseUrl: "http://livetest.kaidez.com/wp-content/themes/kaidez-2012/js",
deps: ['scripts'],
paths: {
jquery: "jquery",
jqueryValidate: "jquery-validate.min"
},
shim: {
jquery: {
exports: "jquery"
},
jqueryValidate: {
deps: ["jquery"]
}
}
});
config.js說它有一個名爲scripts.js的依賴關係,如下所示:
// code that adds buttons to the page
require(['jquery'], function ($) {
var header = document.getElementById("masthead"),
$navMenu = $("#site-navigation-list"),
$searchBox = $("#searchform"),
menuButton = document.createElement("div"),
searchButton = document.createElement("div"),
showMenus;
$(menuButton).attr("id", "menu");
$(searchButton).attr("id", "search");
header.appendChild(searchButton);
header.appendChild(menuButton);
showMenus = function(btn,el) {
$(btn).click(function() {
if (el.is(":visible")) {
el.slideUp({
complete:function(){
$(this).css("display","");
}
});
} else {
el.slideDown();
}
});
};
showMenus(menuButton, $navMenu);
showMenus(searchButton, $searchBox);
});
//code that controls jQuery Validate
require(["jquery", "jqueryValidate"], function($, jqueryValidate) {
$("#contact").validate({
rules: {
form_email: {
required: true,
number: true
}
}
});
});
看着其他發佈的問題,jsFiddles等,我似乎無法找到答案。我認爲我的錯誤出現在我的RequireJS設置中,但我不確定。工作環境代碼是here。任何幫助表示讚賞。
正是我在找的。謝謝! – Mathias