我尝试使用不同的名称(相同数量的字段)对多个表单进行验证,但我无法连接变量。请参阅以下表格:
<form method="post" id="form_validate344" class="form_validate" data-id="344" novalidate="novalidate">
<div class="col-sm-12">
<input type="text" class="form-control" value="" name="email-form-nombre344" id="email-form-nombre344" placeholder="NOMBRE COMPLETO*">
</div>
<div class="col-sm-6">
<input type="text" class="form-control" value="" name="email-form-email344" id="email-form-email344" placeholder="EMAIL*">
</div>
<div class="col-sm-6">
<input type="text" class="form-control" value="" name="email-form-telefono344" id="email-form-telefono344" placeholder="TELÉFONO*">
</div>
<div class="col-sm-8">
<input type="text" class="form-control" value="" name="email-form-empresa344" id="email-form-empresa344" placeholder="EMPRESA">
</div>
<div class="col-sm-4">
<input type="text" class="form-control" value="" name="email-form-edad344" id="email-form-edad344" placeholder="EDAD*">
</div>
<div class="col-sm-8">
<input type="text" class="form-control" value="" name="email-form-puesto344" id="email-form-puesto344" placeholder="PUESTO*">
<input type="hidden" value="344" name="post_id">
</div>
<div class="col-sm-4">
<select name="email-form-sede344" class="form-control" id="email-form-sede344">
<option value="">SEDE*</option><option value="Monterrey">Monterrey</option></select>
</div>
<div class="col-sm-4 col-sm-offset-4">
<input type="submit" class="form-control btn submit" name="enviar344" value="ENVIAR">
</div>
</form>
因为你可以看到每个输入都有一个以数字结尾的名称(在本例中为344),这是在PHP代码中生成不同的其他形式,这里也是我的javascrip来验证:
$('form.form_validate').each(function () {
$( this ).validate( {
rules: {"email-form-nombre344" : "required", },
messages: {
test: "Por favor entra tu usuario",
"email-form-email344" : "Por favor entra un email válido",
"email-form-telefono344" : "Por favor entra un número de telefono válido",
"email-form-empresa344" : "Por favor entra tu empresa",
"email-form-edad344" : "Por favor entra un número de edad válido",
"email-form-puesto344" : "Por favor entra tu puesto",
"email-form-sede344" : "Por elige una sede",
},
errorElement: "em",
errorPlacement: function ( error, element ) {
// Add the `help-block` class to the error element
error.addClass( "help-block" );
// Add `has-feedback` class to the parent div.form-group
// in order to add icons to inputs
//element.parents( ".jv" ).addClass( "has-feedback" );
if ( element.prop( "type" ) === "checkbox" ) {
error.insertAfter( element.parent( "label" ) );
} else {
error.insertAfter( element );
}
// Add the span element, if doesnt exists, and apply the icon classes to it.
if ( !element.next( "span" )[ 0 ] ) {
$( "<span class=\"glyphicon glyphicon-remove form-control-feedback\"></span>" ).insertAfter( element );
}
},
success: function ( label, element ) {
// Add the span element, if doesnt exists, and apply the icon classes to it.
if ( !$( element ).next( "span" )[ 0 ] ) {
$( "<span class=\"glyphicon glyphicon-ok form-control-feedback\"></span>" ).insertAfter( $( element ) );
}
},
highlight: function ( element, errorClass, validClass ) {
$( element ).parents( ".jv" ).addClass( "has-error" ).removeClass( "has-success" );
$( element ).next( "span" ).addClass( "glyphicon-remove" ).removeClass( "glyphicon-ok" );
},
unhighlight: function ( element, errorClass, validClass ) {
$( element ).parents( ".jv" ).addClass( "has-success" ).removeClass( "has-error" );
$( element ).next( "span" ).addClass( "glyphicon-ok" ).removeClass( "glyphicon-remove" );
}
});
});
它可以工作,但只会验证以344结尾的表单,我想验证每个表单。我也使用它来连接变量:
var nombre = "344";
var myObj = {};
myObj["email-form-nombre" + nombre] = {
'required': "true"
};
.
.
.
rules: {
myObj
},
但没有任何作用。怎么做到这一点?
答案 0 :(得分:1)
如果您有许多具有不同表单名称(相同字段数)的表单,那么为什么 动态命名输入名称。 您可以为所有表格输入相似的名称 例如:id =“ form_validate344”的表单可以有以下输入
<input type="text" class="form-control" value="" name="email-form-nombre" id="email-form-nombre344" placeholder="NOMBRE COMPLETO*">
id =“ form_validate345”的表单可以输入以下内容
<input type="text" class="form-control" value="" name="email-form-nombre" id="email-form-nombre345" placeholder="NOMBRE COMPLETO*">
在以上两种情况下,输入名称都是相似的,并且输入ID唯一不同 而其他所有输入都一样
因此,在js代码中,无需提及表单名和ID
您的最终代码如下:
HTML
<form method="post" id="form_validate344" class="form_validate" data-id="344" novalidate="novalidate">
<div class="col-sm-12">
<input type="text" class="form-control" value="" name="email-form-nombre" id="email-form-nombre344" placeholder="NOMBRE COMPLETO*">
</div>
<div class="col-sm-6">
<input type="text" class="form-control" value="" name="email-form-email" id="email-form-email344" placeholder="EMAIL*">
</div>
<div class="col-sm-6">
<input type="text" class="form-control" value="" name="email-form-telefono" id="email-form-telefono344" placeholder="TELÉFONO*">
</div>
<div class="col-sm-8">
<input type="text" class="form-control" value="" name="email-form-empresa" id="email-form-empresa344" placeholder="EMPRESA">
</div>
<div class="col-sm-4">
<input type="text" class="form-control" value="" name="email-form-edad" id="email-form-edad344" placeholder="EDAD*">
</div>
<div class="col-sm-8">
<input type="text" class="form-control" value="" name="email-form-puesto" id="email-form-puesto344" placeholder="PUESTO*">
<input type="hidden" value="344" name="post_id">
</div>
<div class="col-sm-4">
<select name="email-form-sede" class="form-control" id="email-form-sede344">
<option value="">SEDE*</option><option value="Monterrey">Monterrey</option></select>
</div>
<div class="col-sm-4 col-sm-offset-4">
<input type="submit" class="form-control btn submit" name="enviar344" value="ENVIAR">
</div>
</form>
JS
$('.form_validate').each(function () {
$(this).validate( {
//HERE I HAVE REMOVED 344 (form ids)
rules: {"email-form-nombre" : "required", },
//HERE I HAVE REMOVED 344 (form ids)
messages: {
test: "Por favor entra tu usuario",
"email-form-email" : "Por favor entra un email válido",
"email-form-telefono" : "Por favor entra un número de telefono válido",
"email-form-empresa" : "Por favor entra tu empresa",
"email-form-edad" : "Por favor entra un número de edad válido",
"email-form-puesto" : "Por favor entra tu puesto",
"email-form-sede" : "Por elige una sede",
},
errorElement: "em",
errorPlacement: function ( error, element ) {
// Add the `help-block` class to the error element
error.addClass( "help-block" );
// Add `has-feedback` class to the parent div.form-group
// in order to add icons to inputs
//element.parents( ".jv" ).addClass( "has-feedback" );
if ( element.prop( "type" ) === "checkbox" ) {
error.insertAfter( element.parent( "label" ) );
} else {
error.insertAfter( element );
}
// Add the span element, if doesnt exists, and apply the icon classes to it.
if ( !element.next( "span" )[ 0 ] ) {
$( "<span class=\"glyphicon glyphicon-remove form-control-feedback\"></span>" ).insertAfter( element );
}
},
success: function ( label, element ) {
// Add the span element, if doesnt exists, and apply the icon classes to it.
if ( !$( element ).next( "span" )[ 0 ] ) {
$( "<span class=\"glyphicon glyphicon-ok form-control-feedback\"></span>" ).insertAfter( $( element ) );
}
},
highlight: function ( element, errorClass, validClass ) {
$( element ).parents( ".jv" ).addClass( "has-error" ).removeClass( "has-success" );
$( element ).next( "span" ).addClass( "glyphicon-remove" ).removeClass( "glyphicon-ok" );
},
unhighlight: function ( element, errorClass, validClass ) {
$( element ).parents( ".jv" ).addClass( "has-success" ).removeClass( "has-error" );
$( element ).next( "span" ).addClass( "glyphicon-ok" ).removeClass( "glyphicon-remove" );
}
});
});
答案 1 :(得分:0)
您可以相应地更改您的JS
代码:
$('form[id^="form_validate").each(function () {
let thisID = $( this ).attr('id').replace('form_validate','');
$( this ).validate( {
rules: {"email-form-nombre"+thisID : "required", },
messages: {
test: "Por favor entra tu usuario",
"email-form-email"+thisID : "Por favor entra un email válido",
"email-form-telefono"+thisID : "Por favor entra un número de telefono válido",
"email-form-empresa"+thisID : "Por favor entra tu empresa",
"email-form-edad"+thisID : "Por favor entra un número de edad válido",
"email-form-puesto"+thisID : "Por favor entra tu puesto",
"email-form-sede"+thisID : "Por elige una sede",
},
errorElement: "em",
errorPlacement: function ( error, element ) {
// Add the `help-block` class to the error element
error.addClass( "help-block" );
// Add `has-feedback` class to the parent div.form-group
// in order to add icons to inputs
//element.parents( ".jv" ).addClass( "has-feedback" );
if ( element.prop( "type" ) === "checkbox" ) {
error.insertAfter( element.parent( "label" ) );
} else {
error.insertAfter( element );
}
// Add the span element, if doesnt exists, and apply the icon classes to it.
if ( !element.next( "span" )[ 0 ] ) {
$( "<span class=\"glyphicon glyphicon-remove form-control-feedback\"></span>" ).insertAfter( element );
}
},
success: function ( label, element ) {
// Add the span element, if doesnt exists, and apply the icon classes to it.
if ( !$( element ).next( "span" )[ 0 ] ) {
$( "<span class=\"glyphicon glyphicon-ok form-control-feedback\"></span>" ).insertAfter( $( element ) );
}
},
highlight: function ( element, errorClass, validClass ) {
$( element ).parents( ".jv" ).addClass( "has-error" ).removeClass( "has-success" );
$( element ).next( "span" ).addClass( "glyphicon-remove" ).removeClass( "glyphicon-ok" );
},
unhighlight: function ( element, errorClass, validClass ) {
$( element ).parents( ".jv" ).addClass( "has-success" ).removeClass( "has-error" );
$( element ).next( "span" ).addClass( "glyphicon-ok" ).removeClass( "glyphicon-remove" );
}
});
});