开发者

How to disable empty textboxes in a page using JQuery

开发者 https://www.devze.com 2022-12-31 13:04 出处:网络
I h开发者_运维百科ave a page with many panels. each panel will have around 5 textboxes. I need to disable all the textboxes which are empty when the page is loaded. Want to acieve this using JQuery. C

I h开发者_运维百科ave a page with many panels. each panel will have around 5 textboxes. I need to disable all the textboxes which are empty when the page is loaded. Want to acieve this using JQuery. Can somebody help me on this?


This should do it:

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

If you have applied some class to your textboxes, you can also do:

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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消