2017-07-09 20 views
0

我試圖啓用只有當一個複選框被選中一個多重選擇器選擇。我的代碼生成的警報,但不會使選擇錯誤使使用jQuery

選擇&複選框的html +腳本:

Chechbox and selector code

啓用功能:

$("#checkbox").bind("click", function() { 

    if ($('#checkbox').is(":checked")) 
      { alert("checked"); 
      $("#sel").attr('disabled',false); 

      } 
    else 
      { 
      //Do nothing 
      } 
}); 
+1

嗨穆罕默德,歡迎SO。請將代碼始終作爲格式文本發佈,而不是截圖。 – petezurich

+1

您似乎正在使用名爲'multiselect'的插件。這些插件會更改標記並創建其他元素的下拉菜單,這意味着您在此處獲得的任何答案都可能不起作用,因爲您沒有告訴我們任何事情。 – adeneo

+0

@adeneo我有這個教程http://davidstutz.github.io/bootstrap-multiselect/ –

回答

-1

禁止的項目在邏輯上是不可編輯,你需要刪除屬性

element.removeAttribute("disabled"); 
+1

它不是.removeAttribute,它只是.removeAttr – Difster

+0

實際上它既不是,它應該是'element.prop('disabled',false) '因爲它是一個屬性 – adeneo

+0

@Difster Talha是正確的,它的JavaScript不是jQuery。檢查這裏的文檔https://www.w3schools.com/jsref/met_element_removeattribute.asp – Shiladitya

-1

試試這個:使用多選

$("#sel").removeAttr('disabled'); 
0

,試試這個:

$('#example-getting-started').removeAttr('disabled'); 

// And maybe, refresh the plugin 
$('#example-getting-started').multiSelect('refresh'); 
0

對於初學者來說,你使用的ID "sel"爲您jQuery的內選擇,當未中定義您的HTML

所以,我已經先行一步,並改變了對"#example-getting-started"。隨着將禁用屬性的值更改爲"false",而不是false

$(document).on("click", function(e){ 
    if($("#checkbox").is(":checked")){ 
     alert("Checked"); 
     $("#example-getting-started").attr("disabled", "false"); 
    } 
}); 

希望這對您有用! :)