私は、validity.js ( http://validity.thatscaptaintoyou.com/ ) を使用して、contactos.html という名前のファイルにあるフォームを検証しています。
また、css-tricks のチュートリアル「Rethinking Dynamic Page Replaceing Content」( http://css-tricks.com/rethinking-dynamic-page-replacing-content/ ) を使用して、div コンテンツを読み込みます。
このように、contactos.html にあるこのフォームは、jquery を介して index.html ページの div にロードされます。
問題は、validity.js をフォームに適用したいときに、jquery を介して index.html ページにロードされたときに、送信を押すたびにページがリロードされ、検証が行われないことです。
この検証は、フォームの元のページ、つまり contactos.html をロードした場合にのみ機能します。
動的ページ置換コンテンツ コードの再考:
$(window).bind("load", function() {
if(Modernizr.history){
var everPushedSomething = false;
var newHash = "",
$mainContent = $("#content"),
baseHeight = 0,
$el;
$("#selectGoTo select").change(function() {
_link = $(this).val();
//alert(_link)
history.pushState(null, null, _link);
loadContent(_link);
everPushedSomething = true;
return false;
});
$("nav").on("click", "a", function() {
var _link = $(this).attr("href");
//alert(_link);
history.pushState(null, null, _link);
loadContent(_link);
everPushedSomething = true;
return false;
});
function loadContent(href){
$mainContent
.find("#contentDiv")
.fadeOut(400, function() {
$mainContent.hide().load(href + " #contentDiv", function() {
$mainContent.fadeIn(400, function() {
$("#navSubMenu").on("click", "a", function() {
var _link = $(this).attr("href");
//alert(_link);
history.pushState(null, null, _link);
loadContent(_link);
everPushedSomething = true;
return false;
});
if (href === "contactos.html") {
initialize();
$("#newsletter-form").addClass('sky-form');
} else {
};
console.log();
});
/*
$mainContent.fadeIn(200, function() {
$pageWrap.animate({
height: baseHeight + $mainContent.height() + "px"
});
});
$("nav a").removeClass("current");
console.log(href);
$("nav a[href$="+href+"]").addClass("current");
*/
});
});
}
$(window).bind('popstate', function(){
if (everPushedSomething) {
$.getScript(location.href);
_link = location.pathname.replace(/^.*[\\\/]/, ''); //get filename only
}
everPushedSomething = true;
loadContent(_link);
});
} // otherwise, history is not supported, so nothing fancy here.
});
有効性コード:
$("form").validity(function() {
$("#fullname")
.require("O Nome é obrigatório.")
.minLength(12, "O Nome deverá ser mais detalhado.");
$("#email")
.require("O Email é obrigatório.")
.match("email", "Deverá indicar um email válido.");
$("#assunto")
.require();
$("#confirmacao")
.checkboxChecked("Por favor confirme que pretende enviar este email");
//$("#assunto").require("Por favor seleccione um assunto");
});
$.validity.setup({
// You may change the output mode with this property.
outputMode: "summary",
// The this property is set to true, validity will scroll the browser viewport
// so that the first error is visible when validation fails.
scrollTo: false,
// If this setting is true, modal errors will disappear when they are clicked on.
modalErrorsClickable: true,
// If a field name cannot be otherwise inferred, this will be used.
defaultFieldName: "This field"
});
フォームがjqueryを介してロードされたときに、送信ボタンがcontacts.htmlページをロードする代わりにフォームを検証するにはどうすればよいですか
前もって感謝します