It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center 。
8年前关闭。
我在为 jQuery 验证设置规则和消息时遇到问题。
我正在尝试从 resume[zip_code] 中获取值,但我不知道如何在 JQuery 中编写它。我试过了
但不起作用。
这是我需要从中获取值的地方。
应用代码
jQuery
我知道布局是什么,但只是不知道如何检索值
您也不需要将
工作演示:
http://jsfiddle.net/vL7ps/
有关所有方法和规则,请参阅文档:
http://docs.jquery.com/Plugins/Validation
如果您想将该字段验证为必需的美国邮政编码,只需使用
http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/additional-methods.js
jQuery:
演示:http://jsfiddle.net/k6eLf/
8年前关闭。
我在为 jQuery 验证设置规则和消息时遇到问题。
我正在尝试从 resume[zip_code] 中获取值,但我不知道如何在 JQuery 中编写它。我试过了
rules: {
resume[zip_code]: required: true
}
但不起作用。
这是我需要从中获取值的地方。
<div class='control-group'><label class='control-label'>Zip Code: </label>
<div class='controls'>
<input class="required" type='text' name='resume[zip_code]'>
</div>
</div>
应用代码
post '/create_resume' do
session[:errors].clear
@user = User.first(:email_address => session['user'])
@resume = @user.resumes.new(params[:resume])
@resume.save
params[:education].each_key do |school|
@school = @resume.educations.new(params[:education][school]
@school.save
end
params[:job].each_key do |job|
@job = @resume.jobs.new(params[:job][job])
@job.save
end
params[:otherskill].each_key do |other|
@otherskill = @resume.otherskills.new(params[:otherskill][other])
@otherskill.save
end
if session[:errors].empty? === false
erb :resume_form
else
session[:resume_output].clear
erb :resume_output
end
end
jQuery
我知道布局是什么,但只是不知道如何检索值
$(#myForm).validate({
rules: {
something: key => value
},
messages: {
something: "value"
}
});
最佳答案
我不知道如何编写您的规则,因为您从未说明过您想要的规则。仅使用您提供的 HTML,这就是您设置规则和消息的方式。
为了演示用法,我随意制作了最小长度为 5 个数字的字段 required
。
相应地编辑和添加您自己的规则。
$('#myForm').validate({
rules: {
"resume[zip_code]": {
required: true,
minlength: 5,
digits: true
}
},
messages: {
"resume[zip_code]": {
required: "this field is required",
minlength: "this field must contain at least {0} characters",
digits: "this field can only contain numbers"
}
}
});
您也不需要将
class="required"
放在 HTML 中,因为此规则已在您的 jQuery .validate()
方法中指定。<input type='text' name='resume[zip_code]' />
工作演示:
http://jsfiddle.net/vL7ps/
有关所有方法和规则,请参阅文档:
http://docs.jquery.com/Plugins/Validation
如果您想将该字段验证为必需的美国邮政编码,只需使用
zipcodeUS
规则,但您必须首先包含“附加方法”文件 found here 。http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/additional-methods.js
jQuery:
rules: {
"resume[zip_code]": {
required: true,
zipcodeUS: true
}
},
messages: {
"resume[zip_code]": {
required: "this field is required",
zipcodeUS: "this field must contain valid US zip code"
}
}
演示:http://jsfiddle.net/k6eLf/
关于jQuery 验证规则和消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14179417/