2013-03-29 67 views
0

我試圖顯示和隱藏DIV內容取決於用戶的輸入。的onkeyup不工作的hide()

這裏是我的代碼:

<input type="text" name="pass" id="pass" onkeyup="check_input(this.value)"> 

<div id="repeated_pattern" style="display:none"> 
    <b>REPEATED PATTERN</b> 
    <p>Repeated characters</p> 
</div> 

<script> 
function check_input(value){ 
    //this would check if there are 3 or more characters repeated consecutively 
    var repeat_pattern = /(.)\1\1/.test(value); 

    if(repeat_pattern) { 
     $("#repeated_pattern").show(500); 
    }else{ 
     $("#repeated_pattern").hide(500); 
    } 
}  
</script> 

測試:

  1. 當我嘗試輸入gg,股利內容並沒有表現出這樣的結果是OK
  2. 當我嘗試輸入ggg,在div內容顯示這樣的結果是OK
  3. 但是,當我嘗試刪除一個g所以它現在是gg 。它應該是div內容必須隱藏但仍然顯示。 onkeyuphide()函數無法正常工作。

如何使用的onkeyup隱藏()或反之亦然工作?

+0

該瀏覽器是用來 –

+0

似乎這裏http://jsfiddle.net/arunpjohny/KjN6H/工作 –

+0

火狐,等我將嘗試在自己的Chrome – catherine

回答

1

我得到這個錯誤:

ReferenceError: check_input is not defined 

嘗試建立JavaScript中的事件處理程序,使他們在範圍:

http://jsfiddle.net/vHREF/1/

的Javascript:

// Event handlers 
if(document.addEventListener) 
document.getElementById('pass').addEventListener('keyup',check_input,false); 
// Good old Internet Explorer event handling code 
if(document.attachEvent) 
document.getElementById('pass').attachEvent('keyup',check_input); 

function check_input() { 
    var value = $(this).val(); 
    //this would check if there are 3 or more characters repeated consecutively 
    var repeat_pattern = /(.)\1\1/.test(value); 

    if (repeat_pattern) { 
     $("#repeated_pattern").show(500); 
    } else { 
     $("#repeated_pattern").hide(500); 
    } 
} 

HTML:

I'm trying to show and hide div contents depends on the input of the user. Here are my codes: 
<input type="text" name="pass" id="pass"> 
<div id="repeated_pattern" style="display:none"> <b>REPEATED PATTERN</b> 

    <p>Repeated characters</p> 
</div> 
+0

謝謝你現在的作品。 – catherine

+0

@凱瑟琳沒問題。很高興它的工作! –