选择相同输入时禁用输入字段?

时间:2013-03-27 13:07:13

标签: jquery

我有一个输入字段(源语言),当我点击其中一种源语言时,我想要禁用具有相同文本的目标语言

到目前为止,我已经完成了这项工作,但是当我点击源语言

时,它会一直禁用目标语言 每次我想重置的是什么......不要总是听事件

这是演示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());
});

});

4 个答案:

答案 0 :(得分:2)

要重置disabled属性,请将其应用于所有选项,并使用函数调用将其设置为true / false

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

FIDDLE

答案 1 :(得分: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

答案 2 :(得分: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());
            });
        });

答案 3 :(得分:0)

只需删除更改事件的禁用属性...这将删除禁用表单所有选项,稍后您的代码将添加它...

试试这个

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

fiddle here