2013-03-27 133 views
1

我有一個輸入字段(源語言),當我點擊其中一種源語言時,同樣文本的目標語言被禁用選擇相同的輸入時禁用輸入字段?

我做到目前爲止,但它保持禁用目標語言時我點擊源語言

什麼,我想重置,每次..不作總是聽事件

這裏是演示http://jsfiddle.net/ezNxU/

// change the target language according to the source language 
$('#source').change(function() { 

var option_source = $('select#source option:selected').text(); 

$('select#target option').each(function() { 
    if ($(this).text() == option_source) $(this).prop("disabled", true); 
    console.log($(this).text()); 
}); 

}); 

回答

2

要重置disabled屬性,它適用於所有的選項,並用一個函數調用將其設置爲真/假

$('#source').on('change', function() { 
    var self = this; 
    $('#target option').prop('disabled', function() { 
     return this.value == self.value; 
    }); 
}); 

FIDDLE

+0

絕對是最好的! – 2013-03-27 13:24:02

+0

最好簡單的感謝 – osos 2013-03-27 13:34:57

1

試試這個:

$('#source').change(function() { 
    var option_source = $('select#source option:selected').text(); 
    $('select#target option').each(function() { 
     $(this).prop("disabled", ($(this).text() == option_source)); 
    }); 
}); 

DEMO

0

使用這種清除所有

$('select#target option').prop("disabled",false); 

所以你的代碼:Fiddle

$('#source').change(function() { 

      var option_source = $('select#source option:selected').text(); 
       $('select#target option').prop("disabled",false); 
      $('select#target option').each(function() { 

       if ($(this).text() == option_source) $(this).prop("disabled", true); 
       console.log($(this).text()); 
      }); 
     }); 
0

只是改變活動中刪除禁用的屬性.. .that將刪除禁用表單的所有選項,稍後您的代碼將添加它...

試試這個

$('#source').change(function() { 
    $('select#target option').prop("disabled", false); //<-- add this line 
    ..... 

fiddle here