如何使用 Jquery 声明一个函数
how to declare a function using Jquery
我用 javascript 做了一个简单的验证脚本函数,它可以工作,但是当我尝试用 jquery 做它时它不起作用,它没有调用它,我已经找了一些教程但我似乎无法找到明确的答案。
代码如下:
$(document).ready(function () {
function validateForm() {
var cel = document.forms["myForm"]["cel"].value;
var celconf = document.forms["myForm"]["celconf"].value;
var ocr=document.forms["myForm"]["ocr"].value;
var ocrconf = document.forms["myForm"]["ocrconf"].value;
if (cel==""||celconf==""||ocr==""||ocrconf=="") {
alert("Todos los campos deben ser llenados");
}
else if (cel.length != 10||celconf.length!=10) {
alert("El numero celular debe de ser de 10 digitos");}
else if (cel != celconf) {
alert("Los numeros celulares ingresados no Coinciden");
}
else if (ocr.length<10||ocrconf.length<10) {
alert("El OCR ingresado no tiene la longitud necesaria, ésta debe de ser de 13 o 10 digitos");
}
else if (ocr.length > 13 || ocrconf.length > 13) {
alert("El OCR ingresado no tiene la longitud necesaria, ésta debe de ser de 13 o 10 digitos");
}
else if (ocr.length == 11 || ocr.length == 12 || ocrconf == 11 || ocrconf == 12) {
alert("El OCR ingresado no tiene la longitud necesaria, ésta debe de ser de 13 o 10 digitos");
}
else if (ocr != ocrconf) {
alert("Los OCR ingresados no Coinciden");
}
else {
$("#myModal").modal("show");
$.ajax({
type: "POST",
url: "http://localhost:38815/altasms/test",
data: {celjson:cel, ocrjson:ocr},
success: function(obj) {
obj.Celular;
obj.OCR;
}
});
return false;
}
return false;
}
});
我在这里称呼它,形式为:
<form style="padding:7px" name="myForm" method="post" onsubmit="return validateForm()" target="myFrame">
<b>Celular</b><br /><input onblur="validateCel()" class="form-control" type="number" name="cel"><br />
<br /><b>Confirma tu Celular</b><br /> <input class="form-control" type="number" name="celconf"><br />
<br /><b>OCR</b><br /> <input class="form-control" type="number" name="ocr"><br />
<br /><b>Confirma tu OCR</b><br /> <input class="form-control" type="number" name="ocrconf"><br />
<br /><input class="btn btn-success" type="submit" value="Enviar">
</form>
如我所说,它可以与 js 一起使用,但我正在尝试使其与 jquery 一起使用。
validateForm
在此事件处理程序之外不存在:
$(document).ready(function () {
function validateForm() {
// implementation
}
});
所以事件处理程序之外的任何人都看不到它。为了让其他代码使用它,在处理程序之外定义它:
function validateForm() {
// implementation
}
您可以扩展 jQuery 以获得 jQuery 解决方案:
$.extend({
myform: new function () { //create a new object inside jQuery
var _self = this;
_self.initialize = function (form) {
$('.btn-success', form).click(function(){
if (isValid(form)){
submitForm(form);
}
else alert('Invalid Form');
});
};
var isValid = function(form){
//your validation code here
//return false if not valid, true otherwise
return true;
};
var submitForm = function(form){
var cel = $('input[name=cel]', form).val();
var ocr = $('input[name=ocr]', form).val();
$("#myModal").modal("show");
$.ajax({
type: "POST",
url: "http://localhost:38815/altasms/test",
data: {celjson:cel, ocrjson:ocr},
success: function(obj) {
console.log(obj.Celular);
console.log(obj.OCR);
}
});
}
});
$(function () { //initialize on document ready
$.myform.initialize($('form[name=myForm]'));
});
我用 javascript 做了一个简单的验证脚本函数,它可以工作,但是当我尝试用 jquery 做它时它不起作用,它没有调用它,我已经找了一些教程但我似乎无法找到明确的答案。
代码如下:
$(document).ready(function () {
function validateForm() {
var cel = document.forms["myForm"]["cel"].value;
var celconf = document.forms["myForm"]["celconf"].value;
var ocr=document.forms["myForm"]["ocr"].value;
var ocrconf = document.forms["myForm"]["ocrconf"].value;
if (cel==""||celconf==""||ocr==""||ocrconf=="") {
alert("Todos los campos deben ser llenados");
}
else if (cel.length != 10||celconf.length!=10) {
alert("El numero celular debe de ser de 10 digitos");}
else if (cel != celconf) {
alert("Los numeros celulares ingresados no Coinciden");
}
else if (ocr.length<10||ocrconf.length<10) {
alert("El OCR ingresado no tiene la longitud necesaria, ésta debe de ser de 13 o 10 digitos");
}
else if (ocr.length > 13 || ocrconf.length > 13) {
alert("El OCR ingresado no tiene la longitud necesaria, ésta debe de ser de 13 o 10 digitos");
}
else if (ocr.length == 11 || ocr.length == 12 || ocrconf == 11 || ocrconf == 12) {
alert("El OCR ingresado no tiene la longitud necesaria, ésta debe de ser de 13 o 10 digitos");
}
else if (ocr != ocrconf) {
alert("Los OCR ingresados no Coinciden");
}
else {
$("#myModal").modal("show");
$.ajax({
type: "POST",
url: "http://localhost:38815/altasms/test",
data: {celjson:cel, ocrjson:ocr},
success: function(obj) {
obj.Celular;
obj.OCR;
}
});
return false;
}
return false;
}
});
我在这里称呼它,形式为:
<form style="padding:7px" name="myForm" method="post" onsubmit="return validateForm()" target="myFrame">
<b>Celular</b><br /><input onblur="validateCel()" class="form-control" type="number" name="cel"><br />
<br /><b>Confirma tu Celular</b><br /> <input class="form-control" type="number" name="celconf"><br />
<br /><b>OCR</b><br /> <input class="form-control" type="number" name="ocr"><br />
<br /><b>Confirma tu OCR</b><br /> <input class="form-control" type="number" name="ocrconf"><br />
<br /><input class="btn btn-success" type="submit" value="Enviar">
</form>
如我所说,它可以与 js 一起使用,但我正在尝试使其与 jquery 一起使用。
validateForm
在此事件处理程序之外不存在:
$(document).ready(function () {
function validateForm() {
// implementation
}
});
所以事件处理程序之外的任何人都看不到它。为了让其他代码使用它,在处理程序之外定义它:
function validateForm() {
// implementation
}
您可以扩展 jQuery 以获得 jQuery 解决方案:
$.extend({
myform: new function () { //create a new object inside jQuery
var _self = this;
_self.initialize = function (form) {
$('.btn-success', form).click(function(){
if (isValid(form)){
submitForm(form);
}
else alert('Invalid Form');
});
};
var isValid = function(form){
//your validation code here
//return false if not valid, true otherwise
return true;
};
var submitForm = function(form){
var cel = $('input[name=cel]', form).val();
var ocr = $('input[name=ocr]', form).val();
$("#myModal").modal("show");
$.ajax({
type: "POST",
url: "http://localhost:38815/altasms/test",
data: {celjson:cel, ocrjson:ocr},
success: function(obj) {
console.log(obj.Celular);
console.log(obj.OCR);
}
});
}
});
$(function () { //initialize on document ready
$.myform.initialize($('form[name=myForm]'));
});