2012-09-17 60 views
1

我用下面的代碼...使用JavaScript邊框顏色消失

if (theForm.textOne.value.trim() == "" || theForm.textTwo.value.trim() == "") 
{ 
alert("part of the form is blank"); 
document.getElementById("textTwo").style.borderColor="red"; 
} 

的問題是,當警報消失的紅色消失。我如何讓邊框保持這種顏色?

+3

功能完成後execu它不應該刪除樣式婷。你是否在另一個區域設置了'textTwo'元素的'borderColor'風格? –

+0

不,它應該工作正常:http://jsfiddle.net/j08691/6Fqcm/。您的網頁中有其他JavaScript嗎? – j08691

回答

1

爲了邊框apear,你需要比邊框顏色爲它顯示更多:你還需要邊框樣式和邊框寬度

而且它的存在庫,幫助您處理HTML doccument

看看這個jQuery善良:

<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script> 
<script type="text/javascript"> 
    $(function(){ 
     $("form").submit(function() { 
     var valid = true; 
     $("form #textTwo, form #textOne").each(function(){ 
      if ($(this).val().trim() == "") 
      { 
       $(this).css("border","1px #ff0000 solid"); 
       valid = false; 
      } 
     }); 
     if(!valid){ 
      alert("part of the form is blank"); 
      return false; 
     } 
     }); 
    }) 
</script> 

要瘦肉多:http://jquery.com/

+0

謝謝。我忘記了返回錯誤;部分。 –