Javascript Jquery验证器为每个有效()方法添加标签
我对jQuery验证程序插件有一些问题 在与Scriptaculous合作后,我决定摆脱它 但我仍然有问题 每次单击submit按钮时,html中都会添加一个新的标签错误 这都是td标签Javascript Jquery验证器为每个有效()方法添加标签,javascript,jquery,jquery-validate,Javascript,Jquery,Jquery Validate,我对jQuery验证程序插件有一些问题 在与Scriptaculous合作后,我决定摆脱它 但我仍然有问题 每次单击submit按钮时,html中都会添加一个新的标签错误 这都是td标签 <td align="left" colspan="2"> <input class="clase_campo" onfocus="this.className='clase_campo_en_foco';" onblur="this.className='clase_campo';" id=
<td align="left" colspan="2">
<input class="clase_campo" onfocus="this.className='clase_campo_en_foco';" onblur="this.className='clase_campo';" id="CampoDatos" name="CampoDatos" type="text" value="" size="20" maxlength="6" aria-required="true" aria-invalid="true"><label for="CampoDatos" class="error">Debe ingresar un dato</label>
</td>
报价OP:
每次我点击提交按钮,一个新的标签错误就会添加到html。。。另一个问题是onfocus或lostfocus没有清理这些新标签标签,每次我在应该需要的字段中输入一些字符时,错误都不会清除
我不确定你想用这些内联处理程序做什么,你的问题也不清楚。默认情况下,错误标签不会重复,当字段中的数据变为有效/无效时,错误标签会自动切换
如果你只是想要插件的默认功能,而你从来没有解释过它的行为应该如何不同,我认为你太努力了。您的代码是冗余的,过于复杂,并且正在破坏正常行为
回到基本点:
演示:
我的演示中没有内联JavaScript,我只有一个单击处理程序。您只需要单击处理程序,因为您没有使用真正的提交按钮
如您所见,HTML非常基本:
<form id="FormAgregarValor">
<input name="CampoDatos" type="text" />
<input name="CampoImporte" type="text" />
<input id="BotonAceptar" type="button" value="Aceptar" />
</form>
你的代码乱七八糟,你实际上破坏了默认行为,我不理解实际的问题。使用jQuery时不需要任何内联处理程序。在使用Validate插件时,您不需要任何jQuery处理程序,因为这些函数已经内置。请更清楚地解释你想要发生什么,这与插件的默认行为不同。还显示了足够的代码,用于完整的示例,HTML标记在哪里?。在我对你的另一个问题的评论中,我已经向你指出了这个链接:谢谢Sparky,作为一个糟糕的借口,我必须告诉你两件事,一件;我将Jquery添加到一个allready开发的应用程序中,两个;我对jQuery很陌生,这是我想使用的第二个应用程序。在应用程序中,其他程序员使用了大量内联处理程序,我很确定这就是问题的一部分,再次感谢您的澄清。@Nicolas400,如果这解决了您的问题,请接受我的回答。嗨,Sparky,不走运,我想我重新创建了行为。现在我无法提交表单。@Nicolas400,在中,1您忘了将jQuery包含在内,2您在两个选择器中的一个选择器中错误地拼写了FormAgergarValor。它是区分大小写的。试试这个:
<input id="BotonAceptar" class="btn btn-sm btn-default" type="button" name="BotonAceptar" value="Aceptar" title="" onclick="this.disabled=true; /*formAgregarValor.CampoAccformAgregarValor.value='SUBMIT';formAgregarValor.submit();*/" onmouseout="this.style.fontWeight='normal';" onmouseover="this.style.fontWeight='bold';" style="font-weight: bold;">
$( document ).ready(function(){
$('#BotonAceptar').click(function() {
if ( $("#CampoDatos").valid() &&
$("#CampoImporte").valid() ) {
formAgregarValor.CampoAccformAgregarValor.value='SUBMIT';
formAgregarValor.submit();
};
this.disabled=false;
});
$("#formAgregarValor").validate({
rules: {
CampoDatos: "required",
'CampoImporte': {
required: true,
number: true
}
},
messages: {
CampoDatos: {
required: "Debe ingresar un dato"
},
CampoImporte: "Debe ingresar un numero"
}
});
});
$(document).ready(function () {
$('#BotonAceptar').on('click', function(e) { // capture the <button> click
e.preventDefault(); // stop any default <button> action
$("#FormAgregarValor").submit(); // submit form (automatically validates)
});
$("#FormAgregarValor").validate({ // initializes the plugin on your form
rules: {
CampoDatos: "required",
CampoImporte: {
required: true,
number: true
}
},
messages: {
CampoDatos: {
required: "Debe ingresar un dato"
},
CampoImporte: "Debe ingresar un numero"
}
});
});
<form id="FormAgregarValor">
<input name="CampoDatos" type="text" />
<input name="CampoImporte" type="text" />
<input id="BotonAceptar" type="button" value="Aceptar" />
</form>