2014-06-12 35 views
0

我對jQuery Validator插件有一些問題。Jquery Validator爲每個有效的()方法添加標籤

使它與Scriptaculous一起工作後,我決定擺脫它。

但我仍然有問題。

每次我點擊提交按鈕,一個新的標籤錯誤被添加到HTML。

這是所有的td標籤,

<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> 

的我再次點擊提交:

<td align="left" colspan="2"> 
         <input class="clase_campo error" 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><label for="CampoDatos" class="error">Debe ingresar un dato</label> 
      </td> 

,我得到另一個標籤的標籤。

另一個問題是onfocus()或者lostfocus()沒有清理那些新的標籤標籤,每當我在應該需要的字段中輸入一些字符時,錯誤都不清楚。

行爲與樣本不同。

也許我應該消除onbluronfocus屬性來自輸入標籤。

最好的問候。

這是我的輸入按鈕

<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" 
      } 
    }); 
}); 
+0

你的代碼是一個爛攤子(你實際上是打破了默認的行爲),我不知道實際的問題。使用jQuery時,您不需要任何內聯處理程序。在使用Validate插件時,您不需要任何jQuery處理程序,因爲這些函數已經內置。請更清楚地解釋您希望發生的事情與插件的默認行爲不同。還爲完整的示例顯示足夠的代碼(HTML標記在哪裏?)。我已經在你對我的另一個問題的評論中指出了這個鏈接:http://stackoverflow.com/help/mcve – Sparky

回答

0

報價OP

「每次我點擊提交按鈕新的標籤錯誤被添加到HTML ...另一個問題是,onfocus()lostfocus()沒有清理那些新的標籤標籤,每當我在應該要求的字段中輸入一些字符時,錯誤都不清楚。「

我不確定你想要處理所有這些內聯處理程序,你的問題很不清楚。默認情況下,錯誤標籤不會重複,並且當字段中的數據變爲有效/無效時,它們會自動切換。

如果你只是想要插件的默認功能,而你從來沒有解釋它應該有什麼不同的表現,我想你太努力了。 (你的代碼是多餘的,過於複雜,它打破了正常的行爲)

回到最基礎的:

DEMO:http://jsfiddle.net/nK6f3/

我沒有內嵌的JavaScript在我的演示,我已經只有一個click處理程序。您只需要click處理程序,因爲您沒有使用真正的submit按鈕。

$(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" 
     } 
    }); 

}); 

正如你所看到的,HTML是非常基本的:

<form id="FormAgregarValor"> 
    <input name="CampoDatos" type="text" /> 
    <input name="CampoImporte" type="text" /> 
    <input id="BotonAceptar" type="button" value="Aceptar" /> 
</form> 
+0

感謝Sparky,作爲一個不好的藉口,我不得不告訴你兩件事,一件;我將Jquery添加到一個已開發的應用程序中,兩個;我對jQuery相當陌生,這是我想要使用的第二個應用程序。在應用程序中,另一位程序員使用了大量的內聯處理程序,我很確定這是問題的一部分,再次感謝您的澄清。 – Nicolas400

+0

@ Nicolas400,如果這解決了您的問題,請接受我的答案。 – Sparky

+0

嗨,斯帕克,沒有運氣,我想我重新創建行爲,[jsFiddle示例](http://jsfiddle.net/Nicolas400/wn9vr/6/)。現在我無法提交表單。 – Nicolas400

相關問題