2012-05-30 89 views
0

我有一個關於以下問題:當輸入刪除javascript div消失

我有一個div與display:none。一旦用戶在輸入字段中輸入文本(使用jQuery show()方法),就會出現此div。一切工作正常,但一旦我刪除輸入字段中鍵入的文本,我想DIV再次消失。我的嘗試:

使用另一個function與jQuery hide()函數(如果輸入爲空),但它阻止使用show()的函數被觸發(因爲默認輸入顯然是空的)。

任何想法將不勝感激!

這裏是我的腳本:

function password1Security() 

{ 
var pwd1 = document.getElementById("password1"); 
var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g"); 
var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g"); 
var pwdsft = document.getElementById("passwordSafetyLevel1"); 

{ 
if (strongRegex.test(pwd1.value)){pwdsft.innerHTML = '<span style="color:green;">Safe</span>'} else if (mediumRegex.test(pwd1.value)) 

{pwdsft.innerHTML = '<span style="color:orange;">Quite Safe</span>'} else {pwdsft.innerHTML = '<span style="color:red;">Unsafe</span>'}; 
} 
}; 

function passSafetyAppear1() 

{$("#passwordSafety1").show()}; 

function showPassSafety1() 

{ 
password1Security(); 
passSafetyAppear1(); 
}; 

</script> 

<input name="password1" id="password1" type="password" size="15" maxlength="20" style="width:500px; height:30px;" 

onKeyUp="showPassSafety1()"/> 
<br/> 

<span name="passwordSafety1" id="passwordSafety1" style="font-size:12pt; display:none;">Your password is <b 

name="passwordSafetyLevel1" id="passwordSafetyLevel1"></b>.</span><br/> 

謝謝你在前進,

皮埃爾

+0

分享您的實際腳本和標記。 – Gabe

回答

1

請在模糊該操作(當輸入失去焦點)。使用這樣的代碼用正確的選擇來實現這一目標:

$('input-selector').blur(function(){ 
    if(!$(this).val()) 
     $('div-selector').hide(); 
}); 
+0

感謝您的快速回復,我會嘗試:) – pierre1619

+0

我已經嘗試了所有三種可能性,但都沒有工作。是否有可能檢查用戶是否刪除了他在輸入字段中輸入的內容?我尋找過這樣的東西,但迄今爲止沒有取得任何成功。有人有想法嗎? – pierre1619

0

您可以沿着這些路線做一些事情:jsFiddle example

$('input').keyup(function() { 
    if ($(this).val().length > 0) $('div').show(); 
    else $('div').hide(); 
});​ 
0
$('input-selector').keyup(function(){ 
     if ($(this).val().length > 0) 
      $('div-selector').show(); 
     else 
      $('div-selector').hide(); 
}); 

有一個嘗試...