输入删除时,javascript div消失
I have a question concerning the following :
我有一个关于以下问题:
I have a div with display:none
. This div appears once a user types text into an input field (with the jQuery show() method). Everything works fine, but once I delete the text typed into the input field, I would like the DIV to disappear again. My attempt:
我有一个带显示的div:none。一旦用户在输入字段中键入文本(使用jQuery show()方法),就会出现此div。一切正常,但一旦我删除输入字段中输入的文本,我希望DIV再次消失。我的尝试:
use another function
with the jQuery hide()
function if the input is empty, but it prevents the function using show() to be triggered (since the input is obviously empty by default).
如果输入为空,则使用jQuery hide()函数使用另一个函数,但它会阻止使用show()的函数被触发(因为默认情况下输入显然是空的)。
Any ideas would be much appreciated !
任何想法将不胜感激!
Here is my script:
这是我的脚本:
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/>
Thank you in advance,
先谢谢你,
Pierre
皮埃尔
3 个解决方案
#1
1
Do that operation on blur (when the input loses focus). Use code like this with the proper selectors to achieve this:
在模糊时执行该操作(当输入失去焦点时)。使用这样的代码和适当的选择器来实现这个目的:
$('input-selector').blur(function(){
if(!$(this).val())
$('div-selector').hide();
});
更多相关文章
- 运行一次后停止执行函数
- Javascript日期/时间函数是否依赖于客户端机器?
- 是否可以从节点js中的同一模块导出构造函数和一些正常函数?
- 在Javascript中将下拉字段乘以文本字段
- javaES6箭头函数的全新特性
- JavaScript学习-5——异步同步、回调函数
- 什么“返回此”在javascript函数中做什么?
- jQuery和AJAX - 使用Ajax添加的对象动态不适用于jQuery函数?
- Javascript偏函数与柯里化