如何使用JQuery禁用页面中的空文本框

时间:2010-05-19 14:56:01

标签: jquery

我有一个包含许多面板的页面。每个面板将有大约5个文本框。 我需要禁用所有加载页面时为空的文本框。想用JQuery来解决这个问题。有人可以帮我吗?

2 个答案:

答案 0 :(得分:3)

这应该这样做:

$(function(){
 $('input[type="text"]').each(function(){
   if ($(this).val() === '') {
     $(this).attr('disabled', 'disabled');
   }
 });
});

如果您已在文本框中应用了某个类,则还可以执行以下操作:

$(function(){
 $('.class_name').each(function(){
   if ($(this).val() === '') {
     $(this).attr('disabled', 'disabled');
   }
 });
});

答案 1 :(得分:1)

var empties = $('input:text').filter(function() {
    return this.value == '';   // If the value of the input is empty, 
});                            //    add it to the collection

empties.attr('disabled','disabled');   // Then disable the collection of empties