問題
想在submit 按鈕的JS代碼中,檢驗sample_form 的參數,然後提交表單,發現 submit() 函數不起作用怎麼解決?
照著網上查到的例子來寫的,咋就不行呢?
網上的submit例子
我的代碼
$("#form_wizard .button-submit").click(function(h) {
alert("You just finished the wizard. :-)"); // 打印成功
c.attr('action',"user/performTest"); // var c = $("#sample_form"); 見form_wizard.js 文件開頭
c.attr('method',"GET");
alert(c.serialize()); // sample_form的所有參數都成功打印出來了
c.submit(); // 浏覽器的開發工具裡看不到URL,後台也沒有收到URL
alert(c.attr('action') + " " + c.attr('method')); // 兩個變量都成功打印出來了
}).hide();
基本情況:
1,html 文件是 form_wizard.html
2,JS 文件是 form_wizard.js
"use strict";
$(document).ready(function() {
var c = $("#sample_form");
var d = $("#form_wizard");
var a = $(".alert-danger", c);
var f = $(".alert-success", c);
c.validate({
doNotHideMessage: true,
focusInvalid: false,
invalidHandler: function(h, g) {
f.hide();
a.show()
},
submitHandler: function(g) {
f.show();
a.hide();
}
});
var e = function() {
$("#tab4 .form-control-static", c).each(function() {
var g = $('[name="' + $(this).attr("data-display") + '"]', c);
if (g.is(":text") || g.is("textarea")) {
$(this).html(g.val())
} else {
if (g.is("select")) {
$(this).html(g.find("option:selected").text())
} else {
if (g.is(":radio") && g.is(":checked")) {
$(this).html(g.attr("data-title"))
}
}
}
})
};
var b = function(k, g, h) {
/*var str = JSON.stringify(k);
alert(str);
str = JSON.stringify(g);
alert(str);
str = JSON.stringify(h);
alert(str);*/
var l = g.find("li").length;
var m = h + 1;
$(".step-title", d).text("Step " + (h + 1) + " of " + l);
$("li", d).removeClass("done");
var n = g.find("li");
for (var j = 0; j < h; j++) {
$(n[j]).addClass("done")
}
if (m == 1) {
d.find(".button-previous").hide()
} else {
d.find(".button-previous").show()
}
if (m >= l) {
d.find(".button-next").hide();
d.find(".button-submit").show();
e()
} else {
d.find(".button-next").show();
d.find(".button-submit").hide()
}
};
d.bootstrapWizard({
nextSelector: ".button-next",
previousSelector: ".button-previous",
onTabClick: function(i, g, h, j) {
f.hide();
a.hide();
if (c.valid() == false) {
return false
}
b(i, g, j)
},
onNext: function(i, g, h) {
f.hide();
a.hide();
if (c.valid() == false) {
return false
}
b(i, g, h)
},
onPrevious: function(i, g, h) {
f.hide();
a.hide();
b(i, g, h)
},
onTabShow: function(j, g, i) {
var k = g.find("li").length;
var l = i + 1;
var h = (l / k) * 100;
d.find(".progress-bar").css({
width: h + "%"
})
}
});
d.find(".button-previous").hide();
$("#form_wizard .button-submit").click(function(h) {
alert("You just finished the wizard. :-)");
c.attr('action',"user/performTest");
c.attr('method',"GET");
alert(c.serialize());
c.submit();
alert(c.attr('action') + " " + c.attr('method'));
}).hide();
});
c.submit();
==》
c[0].submit();
調用dom對象的submit,而不是jquery對象的submit方法,jquery對象的submit方法由於你使用了validate插件會對表單內容進行驗證的,沒有通過時不會提交表單,用dom對象的則不會驗證表單直接提交