2017-04-10 31 views
0

我想添加如果else語句下面的函數。簡單的if/else點擊功能

else語句將包含以下代碼,以便color_update不僅會被隱藏,而且在未單擊按鈕時會同時被禁用。

$("#color_update").prop('disabled', true); 

這是對碼功能:

JS

function open(elem) { 
    if (document.createEvent) { 
     var e = document.createEvent("MouseEvents"); 
     e.initMouseEvent("mousedown", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); 
     elem[0].dispatchEvent(e); 
    } else if (element.fireEvent) { 
     elem[0].fireEvent("onmousedown"); 
    } 
} 

$(document).ready(function(){ 

    $('#update').click(function(event){ 
     event.preventDefault(); 
     event.stopImmediatePropagation(); 
     $('#color_update').addClass("show").focus(); 
     open($('#color_update')); 
     $("#color").prop('disabled', true); 

    }); 

CSS

#color_update { 
display: none; 
} 

HTML

<select id="color"> 
     <option value="Red">Red</option> 
     <option value="Green">Green</option> 
     <option value="Blue">Blue</option> 
    </select> 

    <input type="button" id="update" value="Update" /> 

    <select id="color_update"> 
     <option value="Red">Black</option> 
     <option value="Green">Purple</option> 
     <option value="Blue">White</option> 
    </select> 

請指導我。謝謝。

+0

你想要什麼? 'color_update'最初被禁用?和「開放」功能如何與問題相關? – Satpal

+0

什麼時候open()被調用?目前沒有在片段? – Zze

+0

我想添加if語句到'$('#update')。click(function(event){'''color_update'在開始時被禁用,只有當點擊更新按鈕時纔會啓用@Satpal –

回答

1

從我所能理解的,你想禁用/ enbable點擊按鈕上的兩個選擇元素之一。 這裏是fiddle爲同一

本質上講,我們可以使用兩種選擇元素的disabled財產的決定因素

$("#color_update").prop('disabled', true); // Disable the select box initially 
    $("#color_update").hide(); // Also hide it 

    $('#update').click(function() { 
     if ($('#color_update').prop('disabled') == true) { // We check the 'disabled' property to determine our next step 

     // The button is now disabled, we need to enable it and hide the #color select 
     $("#color_update").prop('disabled', false); 
     $("#color").prop('disabled', true); 

     // We can now show and hide the respective select elements 
     $("#color_update").show(); 
     $("#color").hide(); 
     } else { // We can now handle the other case 
     $("#color_update").prop('disabled', true); 
     $("#color").prop('disabled', false); 

     $("#color_update").hide(); 
     $("#color").show(); 
     } 
    }); 

不要讓我知道如果你需要別的東西

編輯:更新了jsFiddle鏈接

+0

這正是我' m尋找。謝謝@MayankRaj –

+0

@Mint,很高興幫助。繼續黑客:) –