我試圖快速驗證它並在彈出框中顯示它#result,但是 我不能只添加我的項目一次。我試過e.preventDefault();和e.stopPropagation(); - 兩者都不起作用。 .html不是解決方案,因爲我不想在div中放置兩個(或更多)段落。如何停止他們?動態驗證 - 只有一個追加
我的html代碼:
HTML:
<form name="form" id="form" method="post">
<span> Write pwd: </span>
<input id="login" name="login" type="text" />
</form>
<div id="result" class="tooltip"></div>
和JavaScript:
$(document).ready(function() {
$('#login').keyup(function() {
var th = document.getElementById('login').value;
if (th.length < 6){
$('#result').css('display','inline');
$('#result').append('<p id="too_short">Too short password.</p>');
} else{
$("#too_short").remove();
}
if (th.contains('\'') || th.contains('\"') || th.contains('\\') || th.contains('\/')){
$('#result').css('display','inline');
$('#result').append('<p id="forb_char">Forbidden characters</p>');
} else{
$("#forb_char").remove();
}
});
});
我不知道如果我得到你的問題,是[這](http://jsfiddle.net/qwux7o9r/1/)你想實現什麼? – 2014-11-02 21:59:15
@Valerio當試圖移除警告時,如果它們不存在,這不會引發錯誤嗎? – nicael 2014-11-02 22:04:39
@nicael你說得對,我認爲將靜態HTML中的所有空白div填充然後用html()和empty()方法填充它們並清空它們會更好 – 2014-11-02 22:09:04