2014-02-07 39 views
0

我用這:設置一個變量作爲一個函數內qtip內容的內容

// Built title error 
    function showUsernameError() { 
    $('.addbuildtitle').css('border', 'solid 2px red'); 
    // Show error qTip 
    $('#addbuildtitle').qtip({ 
    content: { 
     text: 'Please enter a build title' 
    }, 
    show: '', 
    style: { 
    classes: 'qtip-red qtip-shadow' 
    }, 
    position: { 
    my: 'top center', // Position my top left... 
    at: 'bottom center', // at the bottom right of... 
    target: $('#addbuildtitle') // my target 
    }, 
    hide: { 
     event: 'unfocus' 
    } 
    }).qtip('show'); 
    } 

我目前做的是在我的Ajax調用:

if (response.emptyTitle === true) { 
     showUsernameError(); 
    } 

其中一期工程,但有可是多個錯誤消息。

我想嘗試在ajax響應中設置qTip內容文本。

我曾嘗試:

if (response.emptyTitle === true) { 
     var errorMessage = 'Test'; 
     showUsernameError(); 
    } 

然後將此功能設置爲:

// Built title error 
function showUsernameError() { 
    $('.addbuildtitle').css('border', 'solid 2px red'); 
    $('#addbuildtitle').qtip({ 
    content: { 
     text: errorMessage 
    }, 
    show: '', 
    style: { 
    classes: 'qtip-red qtip-shadow' 
    }, 
    position: { 
    my: 'top center', // Position my top left... 
    at: 'bottom center', // at the bottom right of... 
    target: $('#addbuildtitle') // my target 
    }, 
    hide: { 
     event: 'unfocus' 
    } 
    }).qtip('show'); 
} 

但是,這並不工作,這只是我的js缺乏知識。我相信它很簡單,我只是無法弄清楚。

謝謝!

+0

你在控制檯中的任何錯誤? showUsenameError是否可以訪問errorMessage var?添加一個斷點,看看在showUsernameError上分配了什麼errorMessage –

+0

該var設置正確,但我有一個未定義的errorMessage。我應該在函數中聲明一些東西嗎? – Lovelock

回答

1

需要將參數傳遞到我的函數中,例如

function showTitleError(errorMessage) { 
    $('.addbuildtitle').css('border', 'solid 2px red'); 
    $('#addbuildtitle').qtip({ 
     content: { text: errorMessage }, 
     show: '', 
     style: { classes: 'qtip-red qtip-shadow' }, 
     position: { my: 'top center', at: 'bottom center', target: $('#addbuildtitle') }, 
     hide: { event: 'unfocus' } 
    }).qtip('show'); 
} 

,然後調用函數時做到:

errorMessage = 'My Error Message'; 
showTitleError(errorMessage);