jquery表单验证提交,使用jquery实现表单验证
怎么使用jquery提交表单
jquery提交表单有两种情况:
10年的崇仁网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都营销网站建设的优势是能够根据用户设备显示端的尺寸不同,自动调整崇仁建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“崇仁网站设计”,“崇仁网站推广”以来,每个客户项目都认真落实执行。
1:jquery只做提交用。
$("form").submit();
这个的表单提交到什么地方的是更具form元素里面的action属性去定义的。
2:jquery用ajax提交数据。
$.ajax({
type:"post",
url:"xxx.php", // 这里是提交到什么地方的url
data:{}, // 这里把表单里面的数据放在这里传到后台
dataType:"json",
success:function(res){
// 调用回调函数
}
});
JQuery实现提交表单时候需要验证所有文本框是否为空吗?
这个可以在表单上添加onsubmit函数和使用jQuery的each函数来判断
varflag=true;
functioncheckForm(frm){
$("#frminput[type='text']").each(function(i,obj){
if(obj.value==""){
alert($(obj).attr("placeholder"));
flag=false;
returnfalse;
}
});
returnflag;
}
#47;script
文本1:
文本2:
文本3:
文本4:
文本5:
这个可以在表单上添加onsubmit函数和使用jQuery的each函数来判断
script type="text/javvascript"
var flag = true;
function checkForm(frm) {
$("#frm input[type='text']").each(function(i, obj) {
if(obj.value == "") {
alert($(obj).attr("placeholder"));
flag = false;
return false;
}
});
return flag;
}
/script
form ation="目标地址" method="post" onsubmit="return checkForm();" id="frm"
文本1:input type="text" name="txt1" value="" placeholder="文本1不能为空"/br/
文本2:input type="text" name="txt2" value="" placeholder="文本2不能为空"/br/
文本3:input type="text" name="txt3" value="" placeholder="文本3不能为空"/br/
文本4:input type="text" name="txt4" value="" placeholder="文本4不能为空"/br/
文本5:input type="text" name="txt5" value="" placeholder="文本5不能为空"/br/
input type="submit" value="提交表单"/
jquery在表单提交前有几种校验方法
在表单提交前进行验证的几种方式 .
在Django中,为了减轻后台压力,可以利用JavaScript在表单提交前对表单数据进行验证。下面提供了有效的几种方式(每个.html文件为一种方式)。
formpage1.html
复制代码 代码如下:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8" /
titleExample1/title
script type="text/javascript" src="/Resource/jquery-1.4.1.js"/script
script type="text/javascript"
function jump()
{
//清空表单所有数据
document.getElementById("firstname").value=""
document.getElementById("lastname").value=""
$("#firstnameLabel").text("")
$("#lastnameLabel").text("")
}
$(document).ready(function(){
$("#form1").bind("submit", function(){
var txt_firstname = $.trim($("#firstname").attr("value"))
var txt_lastname = $.trim($("#lastname").attr("value"))
$("#firstnameLabel").text("")
$("#lastnameLabel").text("")
var isSuccess = 1;
if(txt_firstname.length == 0)
{
$("#firstnameLabel").text("firstname不能为空!")
$("#firstnameLabel").css({"color":"red"});
isSuccess = 0;
}
if(txt_lastname.length == 0)
{
$("#lastnameLabel").text("lastname不能为空!")
$("#lastnameLabel").css({"color":"red"});
isSuccess = 0;
}
if(isSuccess == 0)
{
return false;
}
})
})
/script
/head
body
提交表单前进行验证(方法一)
hr width="40%" align="left" /
form id="form1" method="post" action="/DealWithForm1/"
table
tr
tdfirst_name:/td
tdinput name="firstname" type="text" id="firstname" //td
tdlabel id="firstnameLabel"/label/td
/tr
tr
tdlast_name:/td
tdinput name="lastname" type="text" id="lastname" //td
tdlabel id="lastnameLabel"/label/td
/tr
/table
hr width="40%" align="left" /
button type="submit"提交/button
button type="button" onclick="jump();"取消/button
/form
/body
/html
formpage2.html
复制代码 代码如下:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8" /
titleExample2/title
script type="text/javascript" src="/Resource/jquery-1.4.1.js"/script
script type="text/javascript"
function jump()
{
//清空表单所有数据
document.getElementById("firstname").value=""
document.getElementById("lastname").value=""
$("#firstnameLabel").text("")
$("#lastnameLabel").text("")
}
function check(){
var txt_firstname = $.trim($("#firstname").attr("value"))
var txt_lastname = $.trim($("#lastname").attr("value"))
$("#firstnameLabel").text("")
$("#lastnameLabel").text("")
var isSuccess = 1;
if(txt_firstname.length == 0)
{
$("#firstnameLabel").text("firstname不能为空!")
$("#firstnameLabel").css({"color":"red"});
isSuccess = 0;
}
if(txt_lastname.length == 0)
{
$("#lastnameLabel").text("lastname不能为空!")
$("#lastnameLabel").css({"color":"red"});
isSuccess = 0;
}
if(isSuccess == 0)
{
return false;
}
return true;
}
/script
/head
body
提交表单前进行验证(方法二)
hr width="40%" align="left" /
form id="form1" method="post" action="/DealWithForm1/" onsubmit="return check()"
table
tr
tdfirst_name:/td
tdinput name="firstname" type="text" id="firstname" //td
tdlabel id="firstnameLabel"/label/td
/tr
tr
tdlast_name:/td
tdinput name="lastname" type="text" id="lastname" //td
tdlabel id="lastnameLabel"/label/td
/tr
/table
hr width="40%" align="left" /
button type="submit"提交/button
button type="button" onclick="jump();"取消/button
/form
/body
/html
formpage3.html
复制代码 代码如下:
!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ""
html xmlns=""
head
meta http-equiv="Content-Type" content="text/html; charset=utf-8" /
titleExample3/title
script type="text/javascript" src="/Resource/jquery-1.4.1.js"/script
script type="text/javascript"
function jump()
{
//清空表单所有数据
document.getElementById("firstname").value=""
document.getElementById("lastname").value=""
$("#firstnameLabel").text("")
$("#lastnameLabel").text("")
}
function checktosubmit(){
var txt_firstname = $.trim($("#firstname").attr("value"))
var txt_lastname = $.trim($("#lastname").attr("value"))
$("#firstnameLabel").text("")
$("#lastnameLabel").text("")
var isSuccess = 1;
if(txt_firstname.length == 0)
{
$("#firstnameLabel").text("firstname不能为空!")
$("#firstnameLabel").css({"color":"red"});
isSuccess = 0;
}
if(txt_lastname.length == 0)
{
$("#lastnameLabel").text("lastname不能为空!")
$("#lastnameLabel").css({"color":"red"});
isSuccess = 0;
}
if(isSuccess == 1)
{
form1.submit();
}
}
/script
/head
body
提交表单前进行验证(方法三)
hr width="40%" align="left" /
form id="form1" method="post" action="/DealWithForm1/"
table
tr
tdfirst_name:/td
tdinput name="firstname" type="text" id="firstname" //td
tdlabel id="firstnameLabel"/label/td
/tr
tr
tdlast_name:/td
tdinput name="lastname" type="text" id="lastname" //td
tdlabel id="lastnameLabel"/label/td
/tr
/table
hr width="40%" align="left" /
button type="button" onclick="checktosubmit()"提交/button
button type="button" onclick="jump();"取消/button
/form
/body
/html
以下是视图函数、URL配置以及相关设置
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
views.py
复制代码 代码如下:
#coding: utf-8
from django.http import HttpResponse
from django.shortcuts import render_to_response
def DealWithForm1(request):
if request.method=="POST":
FirstName=request.POST.get('firstname','')
LastName=request.POST.get('lastname','')
if FirstName and LastName:
response=HttpResponse()
response.write("htmlbody"+FirstName+" "+LastName+u"! 你提交了表单!/body/html")
return response
else:
response=HttpResponse()
response.write('htmlscript type="text/javascript"alert("firstname或lastname不能为空!");\
window.location="/DealWithForm1"/script/html')
return response
else:
return render_to_response('formpage1.html')
def DealWithForm2(request):
if request.method=="POST":
FirstName=request.POST.get('firstname','').encode("utf-8")
LastName=request.POST.get('lastname','').encode("utf-8")
if FirstName and LastName:
html="htmlbody"+FirstName+" "+LastName+"! 你提交了表单!"+"/body/html"
return HttpResponse(html)
else:
response=HttpResponse()
response.write('htmlscript type="text/javascript"alert("firstname或lastname不能为空!");\
window.location="/DealWithForm2"/script/html')
return response
else:
return render_to_response('formpage2.html')
def DealWithForm3(request):
if request.method=="POST":
FirstName=request.POST.get('firstname','')
LastName=request.POST.get('lastname','')
if FirstName and LastName:
response=HttpResponse()
response.write('htmlbody'+FirstName+LastName+u'! 你提交了表单!/body/html')
return response
else:
response=HttpResponse()
response.write('htmlscript type="text/javascript"alert("firstname或lastname不能为空!");\
window.location="/DealWithForm3"/script/html')
return response
else:
return render_to_response('formpage3.html')
urls.py
复制代码 代码如下:
from django.conf.urls.defaults import patterns, include, url
import views
from django.conf import settings
urlpatterns = patterns('',
url(r'^Resource/(?Ppath.*)$','django.views.static.serve',{'document_root':settings.STATIC_RESOURCE}),
url(r'^DealWithForm1','views.DealWithForm1'),
url(r'^DealWithForm2','views.DealWithForm2'),
url(r'^DealWithForm3','views.DealWithForm3'),
)
settings.py
复制代码 代码如下:
# Django settings for CheckFormBeforeSubmit project.
import os
HERE = os.path.abspath(os.path.dirname(__file__))
DEBUG = True
TEMPLATE_DEBUG = DEBUG
...
STATIC_RESOURCE=os.path.join(HERE, "resource")
...
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.csrf.CsrfResponseMiddleware',
)
ROOT_URLCONF = 'CheckFormBeforeSubmit.urls'
TEMPLATE_DIRS = (
os.path.join(HERE,'template'),
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
分享题目:jquery表单验证提交,使用jquery实现表单验证
链接URL:http://ybzwz.com/article/dssgiih.html