选择相同的输入时禁用输入字段?
问题描述:
我有一个输入字段(源语言),当我点击其中一种源语言时,同样文本的目标语言被禁用选择相同的输入时禁用输入字段?
我做到目前为止,但它保持禁用目标语言时我点击源语言
什么,我想重置,每次..不作总是听事件
这里是演示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());
});
});
答
要重置disabled属性,它适用于所有的选项,并用一个函数调用将其设置为真/假
$('#source').on('change', function() {
var self = this;
$('#target option').prop('disabled', function() {
return this.value == self.value;
});
});
答
试试这个:
$('#source').change(function() {
var option_source = $('select#source option:selected').text();
$('select#target option').each(function() {
$(this).prop("disabled", ($(this).text() == option_source));
});
});
答
使用这种清除所有
$('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());
});
});
答
只是改变活动中删除禁用的属性.. .that将删除禁用表单的所有选项,稍后您的代码将添加它...
试试这个
$('#source').change(function() {
$('select#target option').prop("disabled", false); //<-- add this line
.....
绝对是最好的! – 2013-03-27 13:24:02
最好简单的感谢 – osos 2013-03-27 13:34:57