2017-08-10 61 views
1

我有一個選擇(我曾嘗試標準,以及MaterializeCSS版本和相同的結果)選擇的onchange內部功能將無法運行

<select onchange="changeToolBar(this.value)" id="select_toolbar" class="browser-default"> 
    <option value='0'>Basic</option> 
    <option value='1'>Full</option> 
    <option value='2'>All</option> 
    <option value='3'>Pure</option> 
</select> 

changeToolBar Function()

function changeToolBar(expanded) { 

    if (expanded === 1) { 
    CKEDITOR.instances.editor.destroy();//destroy the existing editor 
    CKEDITOR.replace('editor', {toolbar: 'Full'}); 
    expanded = 2; 
    $("#barOutput").text('Pure'); 
    watchForChanges(); 
    } else if (expanded === 2) { 
    CKEDITOR.instances.editor.destroy();//destroy the existing editor 
    CKEDITOR.replace('editor', {toolbar: 'Word', removeButtons: 'About'}); // Toolbar 'full' is a default one 
    expanded = 3; 
    $("#barOutput").text('Word'); 
    watchForChanges(); 
    } else if (expanded === 3) { 
    CKEDITOR.instances.editor.destroy();//destroy the existing editor 
    CKEDITOR.replace('editor', {toolbar: 'Pure'}); // Toolbar 'full' is a default one 
    expanded = 0; 
    $("#barOutput").text('Full'); 
    watchForChanges(); 
    } else if (expanded === 0) { 
    CKEDITOR.instances.editor.destroy();//destroy the existing editor 
    CKEDITOR.replace('editor', {toolbar: 'Basic'}); // Toolbar 'full' is a default one 
    $("#barOutput").text('Minimalist'); 
    watchForChanges(); 
    } 
} 

此功能將目前從未嘗試觸發。它目前不在$(document.ready()之內,但是我也根據另一篇SO文章的輸入做了嘗試,但沒有任何區別。

我讀到MaterialiseCSS中斷select onchange,所以我嘗試了browser-default並得到了相同的結果。

我後來決定用標準的jQuery on.change

$('#select_toolbar').on('change', function(){ 
    changeToolBar(this.value); 
    console.log(this.value); 
    console.log("Hi"); 
}) 

這火 - 控制檯日誌應有的價值等等,應有盡有。但是,這個函數不會受到影響。

雖然changeToolBar函數看起來很奇怪,但如果我將其綁定到通過changeToolBar(0)等的單個按鈕,它實際上可以工作,所以我不相信這是一個問題。

我必須錯過某個函數中的函數是如何處理的。

作爲一個例子,這裏是一個fiddle。這裏的行爲方式是一樣的。

回答

2

元素的值始終是一個字符串,它從來不是數字1或任何數字。

你必須要麼做不嚴格比較

if (expanded == 1) { 

或轉換爲數字

changeToolBar(+this.value) 

在你撥弄你有類barOutput的元素,你會選擇與

$(".barOutput") 

FIDDLE

+0

啊,謝謝你!我認爲它會與此類似,並且如果它影響了它,它將開始Google搜索。甚至沒有想過只用2'='與'exact'進行比較。再次感謝〜這是完美的。 – DNorthrup

+0

不客氣! – adeneo