2014-03-13 67 views
4

我試圖通過使用Tooltipster插件用戶的get和我運行自定義錯誤消息爲以下問題:jQuery驗證插件和Tooltipster插件錯誤

You called Tooltipster's "content" method on an uninitialized element

我的HTML代碼:

<form> 
    <input id="one" type="number" name="one" /> 
    <input id="two" type="number" name="two" /> 
    <button id="button" type="submit">Submit</button> 
</form> 

的Javascript:

$(document).ready(function() { 

      $('form').validate({ // initialize the plugin 
       rules: { 
        one: { 
         required: true, 
         min: 1, 
         max: 100 
        }, 
        two: { 
         required: true, 
         min: 50, 
         max: 80 
        } 
       }, 
       submitHandler: function (form) { 
        doTest(); 
        return false; 
       }, 
       errorPlacement: function (error, element) { 
        var lastError = $(element).data('lastError'), 
         newError = $(error).text(); 

        $(element).data('lastError', newError); 

        if (newError !== '' && newError !== lastError) { 
         $(element).tooltipster('content', newError); 
         $(element).tooltipster('show'); 
        } 
       }, 
       success: function (label, element) { 
        $(element).tooltipster('hide'); 
       } 
      }); 

     }); 

可能是什麼問題?

回答

7

控制檯錯誤:

You called Tooltipster's "content" method on an uninitialized element

的問題是,你有沒有正確您的輸入元素初始化的Tooltipster插件。

// initialize tooltipster on form input elements 
$('form input').tooltipster({ // <- USE THE PROPER SELECTOR FOR YOUR INPUTs 
    trigger: 'custom', // default is 'hover' which is no good here 
    onlyOne: false, // allow multiple tips to be open at a time 
    position: 'right' // display the tips to the right of the element 
}); 

參見:https://stackoverflow.com/a/14741689/594235

+0

我仍然得到同樣的錯誤。 – kalpetros

+0

@kalpetros,[你可以看到,它的工作原理沒有錯誤](http://jsfiddle.net/2DUX2/3/)。 – Sparky

+1

@kalpetros,你必須確保你正確定位你的輸入元素。嘗試改變'$('form input [type =「text」]')。tooltipster(...'to'$('form input')。tooltipster(...' – Sparky