2012-03-19 112 views
0

鑑於此html: code 我想在選擇d1導致選項2中的b選擇不可用(刪除)。但是,如果頁面刷新或在d1中選擇了任何其他選項,那麼r1中的所有選項都應該可用。jquery刪除條目選擇下拉列表,如果在另一個下拉列表中選擇

這是可能的JQuery的? http://jsfiddle.net/dwvYY/6/

我遇到的一個最初的問題是我似乎無法使用我的客戶端工具(SharePoint放置在Select標籤上)的title屬性來引用select。

謝謝。

回答

2

您可以通過禁用索引選項或隱藏它。 這裏是我的解決方案: http://jsfiddle.net/PabZk/

通過索引選擇選項在大型列表中是一個好主意。

+0

非常不錯..禁用工程太棒了。 – o365spo 2012-03-28 16:13:00

2

也許這段代碼給你一個線索如何實現它,你應該考慮使用一個switch語句intsted。

如果選b 3號將在其他選擇列表被禁用,看看這個小提琴:http://jsfiddle.net/dwvYY/7/

$("select[title$=d1]").change(function(){ 
    // Reset the options: 
    $("select[title$=r1]").children("option").removeAttr("disabled","disabled") 
    var selectedValue = $(this).val(); 
    if(selectedValue =="b"){ 

     // This will disable one option    
     $("select[title$=r1]").children("option[value='3']").attr("disabled","disabled") 
    } 
}) 
​ 
0

這應該回答兩個提問。首先,在您的jsFiddle中,您的代碼正在文檔的頭部運行,而不是onLoad或onDOMready。第二,當從第一個列表中選擇「b」時,該代碼應該按照你所要求的從第二列表中去除「2」的方式進行:

jsFiddle example

的jQuery:

$('select[title$=r1] option:eq(0)').text("Please Select").val(""); 
$('select[title$=d1] ').click(function() { 
    if ($(this).val() == 'b') { 
     $('select[title$=r1] option[value="2"]').remove(); 
    } 
});​ 
0

你需要以監聽change事件的值來動態改變:

$('select[title="rl"]').change(function() { 
    var $this = $(this); 
    var options = $('' + $this + ' option'); 
    options.each(function() { 
    // perform actions on each option value 
    }); 
}); 
0

你可以只顯示/隱藏需要在change事件有條件選項的select元素。嘗試這個。

var $selectr1 = $('select[title=r1]'); 
$('select[title=d1]').change(function(){ 
    $selectr1.find("option").show(); 
    if(this.value == "b"){ 
     $selectr1.find("option[value='2']").hide(); 
    } 
}); 

工作演示 - http://jsfiddle.net/dwvYY/8/

1
$(function(){ 

    $("select[title=d1]").change(function(){ 

     if($(this).val()=="b") 
     { 
      $("select[title=r1] option[value='2']").remove(); 
     }   
     else 
     { 
       $("select[title=r1] option[value='2']").remove();    
       $("select[title=r1] option[value='1']").after($("<option></option>").attr("value","2").text("2")); 
     }    
    });   
})​ 

工作樣本:http://jsfiddle.net/r34Kd/9/

+0

對我的sharePoint newform非常有用,無需將其設置爲自定義。謝謝。 – o365spo 2012-03-28 16:14:08

+0

@cyberpine:很高興知道它的工作。 – Shyju 2012-03-28 16:14:52

相關問題