我看过这个question/answer ,还有更多类似的东西,但我仍然无法通过表单验证来切换选项卡或移动到出现错误的选项卡。
相关 jQuery:
$(document).ready(function () {
$("form[name=modify]").validate({
ignore: [],
rules: {
first_name: {
required: true,
},
surname: {
required: true
},
id_number: {
required: true
},
mobile_number: {
number: true,
minlength: 10,
},
home_number: {
number: true,
minlength: 10,
},
other_contact: {
number: true,
minlength: 10,
},
line1: {
required: true,
},
city_town: {
required: true,
},
postal_code: {
required: true,
minlength: 4,
},
curr_renumeration: {
required: true,
number: true,
},
expect_renumeration: {
required: true,
number: true
},
email: {
email: true,
required: true,
},
highlight: function (label) {
$(label).closest('.control-group').addClass('has-error');
console.log($(label).closest('.control-group').addClass('has-error'));
if ($(".tab-content").find("div.tab-pane.active:has(div.has-error)").length == 0) {
$(".tab-content").find("div.tab-pane:hidden:has(div.has-error)").each(function (index, tab) {
var id = $(tab).attr("id");
$('a[href="#' + id + '"]').tab('show');
});
$('#myTab').on('shown', function (e) {
console.log(this);
e.target // activated tab
$($(e.target).attr('href')).find("div.has-error :input:first").focus();
});
}
},
}
});
$("[type=submit]").submit(function (e) {
e.preventDefault();
});
});
可以查看here
我错过了什么?
有人可以帮帮我吗?我无法让它工作。
更新:
好吧,我已经移动了一些代码并玩了一下:
$("form[name=modify]").validate({
highlight : function(label) {
$(label).closest('.form-group').addClass('has-error');
$(".tab-content").find("fieldset.tab-pane:has(has-error)").each(function(index, tab) {
alert("error from if");
if ($(".tab-content").find("field.tab-pane.active:has(has-error)").length == 0) {
alert("error from each");
var id = $(tab).attr("id");
console.log(id);
$('a[href="#' + id + '"]').tab('show');
}
});
},
invalidHandler : function(event, validator) {
// 'this' refers to the form
$('#myTab').on('shown', function(e) {
console.log(this);
e.target;// activated tab
$($(e.target).attr('href')).find("fieldset.has-error :input:first").focus();
});
var errors = validator.numberOfInvalids();
if (errors) {
var message = errors == 1 ? 'You missed 1 field. It has been highlighted' : 'You missed ' + errors + ' fields. They have been highlighted';
$("div.has-error span").html(message);
$("div.has-error").show();
} else {
$("div.has-error").hide();
}
},
ignore : [],
rules : {
first_name : {
required : true,
},
surname : {
required : true
},
id_number : {
required : true
},
mobile_number : {
number : true,
minlength : 10,
},
home_number : {
number : true,
minlength : 10,
},
other_contact : {
number : true,
minlength : 10,
},
line1 : {
required : true,
},
city_town : {
required : true,
},
postal_code : {
required : true,
minlength : 4,
},
curr_renumeration : {
required : true,
number : true,
},
expect_renumeration : {
required : true,
number : true
},
email : {
email : true,
required : true,
},
}
});
一切正常,除了它在这一行停止 if ($(".tab-content").find("field.tab-pane.active:has(has-error)").length = = 0) {
如果我用 $(".tab-content").find("fieldset.tab-pane:has(has-error)").each(function(index, tab) { 它也停止了...
已更新 jsFiddle
为什么它不进入 .each 或 thif ?
最佳答案
我认为您的问题可能是默认情况下 jquery 只会验证可见字段。所以你需要做的是告诉 jquery 不要忽略你的隐藏字段(其他选项卡)。这可以按如下方式完成。
$("#form1").validate({ ignore: "" });
默认情况下,忽略 :hidden。请参阅此答案和此文档(选项 -> 忽略)。
关于javascript - jQuery 验证不适用于 Bootstrap 3 选项卡式表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20562229/
给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru
我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..
我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val
我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
我有一些非常大的模型,我必须将它们迁移到最新版本的Rails。这些模型有相当多的验证(User有大约50个验证)。是否可以将所有这些验证移动到另一个文件中?说app/models/validations/user_validations.rb。如果可以,有人可以提供示例吗? 最佳答案 您可以为此使用关注点:#app/models/validations/user_validations.rbrequire'active_support/concern'moduleUserValidationsextendActiveSupport:
当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
这里有一个很好的答案解释了如何在Ruby中下载文件而不将其加载到内存中:https://stackoverflow.com/a/29743394/4852737require'open-uri'download=open('http://example.com/image.png')IO.copy_stream(download,'~/image.png')我如何验证下载文件的IO.copy_stream调用是否真的成功——这意味着下载的文件与我打算下载的文件完全相同,而不是下载一半的损坏文件?documentation说IO.copy_stream返回它复制的字节数,但是当我还没有下
rails中是否有任何规定允许站点的所有AJAXPOST请求在没有authenticity_token的情况下通过?我有一个调用Controller方法的JqueryPOSTajax调用,但我没有在其中放置任何真实性代码,但调用成功。我的ApplicationController确实有'request_forgery_protection'并且我已经改变了config.action_controller.consider_all_requests_local在我的environments/development.rb中为false我还搜索了我的代码以确保我没有重载ajaxSend来发送