2012-03-26 76 views
0

我有一個textfield,此刻我禁用了提交按鈕時textfieldhold three charactes。我現在想要做的也是概述textfield大膽的紅色,仍然有submit按鈕被禁用,但我似乎無法同時勾勒出textfield並禁用提交buttonjQuery輪廓文本框顯示錯誤

我的禁用提交按鈕的代碼如下是有可能能夠勾畫出textfieldlength is < 3這個函數的作用嗎?

心存感激的任何幫助

$(function() { 

     $('input[name="updateMessage"]').attr('disabled','disabled'); 
     $('input[name="selectMessageForUpdate"]').keyup(function(){ 
      if($('input[name="selectMessageForUpdate"]').val().length < 3) 
      { 
       $('input[name="updateMessage"]').attr('disabled','disabled'); 
      } 
      else 
      { 
       $('input[name="updateMessage"]').removeAttr('disabled'); 
      } 
     }); 

    }); 

回答

0

添加border CSS屬性隨attr('disabled')

添加一個類來定義紅色邊框,在你的CSS:

textarea[name=selectMessageForUpdate].disabled { border:5px solid red } 

您的JS:

$(function() { 

     $('input[name="updateMessage"]').attr('disabled','disabled'); 
     $('textarea[name="selectMessageForUpdate"]').keyup(function(){ 
      if($(this).val().length < 3) 
      { 
       $('input[name="updateMessage"]').attr('disabled','disabled'); 
       $(this).addClass('disabled'); // ADD THIS 
      } 
      else 
      { 
       $('input[name="updateMessage"]').removeAttr('disabled'); 
       $(this).removeClass('disabled'); // ADD THIS 
      } 
     }); 

    }); 

我創建了一個爵士小提琴和它似乎工作...

http://jsfiddle.net/tmjaF/8/

+0

啊,是這個工程的按鈕,然後但我想勾勒出'textfield'當字符是'<3' @andreas – CodersSC 2012-03-26 09:09:34

+0

確定編輯我的答案 – 2012-03-26 09:10:44

+0

嗯這仍然不工作:/ – CodersSC 2012-03-26 09:34:25

0

他想要的風格textarea的,而不是提交按鈕。只需使用指定的標記向textarea添加一個類,並在輸入有效時將其刪除。同時檢查輸入是否不僅僅是空白。

$(function() { 
    $('input[name=updateMessage]').attr('disabled','disabled'); 
    $('input[name=selectMessageForUpdate]').keyup(function(){ 
     if($('input[name=selectMessageForUpdate]').val().length < 3) 
     { 
      $('input[name=updateMessage]').attr('disabled','disabled'); 
      $('input[name=selectMessageForUpdate]').addClass('someClass'); 
     } 
     else 
     { 
      $('input[name="updateMessage"]').removeAttr('disabled'); 
      $('input[name=selectMessageForUpdate]').removeClass('someClass'); 
     } 
    }); 

}); 
0

添加到你的CSS:

input.indicated_textfield { border: 2px solid red; } 

的addClass與removeClass行添加到您的Javascript代碼:

$(function() { 

    $('input[name="updateMessage"]').prop('disabled', true); 
    $('input[name="selectMessageForUpdate"]').keyup(function(){ 
     if($('input[name="selectMessageForUpdate"]').val().length < 3) 
     { 
      $('input[name="updateMessage"]').prop('disabled', true); 
      $('input[name="selectMessageForUpdate"]').addClass('indicated_textfield'); 
     } 
     else 
     { 
      $('input[name="updateMessage"]').prop('disabled', false); 
      $('input[name="selectMessageForUpdate"]').removeClass('indicated_textfield'); 
     } 
    }); 

}); 
+0

我正在使用jquery-1.7.2.js @elaxsj – CodersSC 2012-03-26 09:16:49

+0

在jQuery 1.6.1+中,您應該使用prop方法代替attr/removeAttr。 prop方法是1.6.1中的新增功能,用於在頁面加載後更改屬性。我相應地更新了我的代碼:) – elaxsj 2012-03-26 09:20:18

+0

感謝那,殘疾人仍然工作,但'textfield'的輪廓不起作用:/我已檢查'CSS'已加載,但它仍然沒有顯示在'textfield '@elaxsj – CodersSC 2012-03-26 09:23:08