Javascript jquery - 如何向此 jquery 代码库添加警报消息?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11474138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jquery - How do I add an alert message to this jquery code base?
提问by user1176783
The Question
问题
To help avoid end-user confusion, I want to add an alert message that pops up if/when the user clicks any other key ["alert('Only Numerical data allowed')"]. So if they press the 'k' the above message will pop up. Can anyone see how to set this code within this code base
为了帮助避免最终用户混淆,我想添加一条警告消息,当用户单击任何其他键时/当该消息弹出 ["alert('Only Numerical data allowed')"]。因此,如果他们按“k”,则会弹出上述消息。谁能看到如何在此代码库中设置此代码
- jsfiddle: http://jsfiddle.net/EN8pT/4/
- jsfiddle:http: //jsfiddle.net/EN8pT/4/
The Code
编码
jquery:
查询:
$('input.numberinput').bind('keypress', function (e) {
var w = e.which;
return (w != 8 && w != 0 && (w < 48 || w > 57) && w != 46) ? false : true;
});
?
?
html
html
<div class="containercontent">
<div class="label">Enter a number:</div>
<input type="text" name="txtNumber1" id="txtNumber1" value="" class="numberinput" />
<div class="label">Enter a number:</div>
<input type="text" name="txtNumber2" id="txtNumber2" value="" class="numberinput" />
</div>
采纳答案by Daniel Li
Hello again :) I can help you out with this as well:
你好 :) 我也可以帮你解决这个问题:
$(document).ready(function () {
$('input.numberinput').bind('keypress', function (e) {
var allow = (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57) && e.which != 46) ? false : true;
if (!allow) {
alert('Only Numerical data allowed');
}
return allow;
});
});?
JSFiddle: http://jsfiddle.net/EN8pT/3/
JSFiddle:http: //jsfiddle.net/EN8pT/3/
Enjoy and good luck!
享受并祝你好运!
回答by Suave Nti
Simple :
简单的 :
$(document).ready(function () {
$('input.numberinput').bind('keypress', function (e) {
if((e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57) && e.which != 46) )
{
alert('Only Numbers');
return false;
}
else{
return true;
}
});
});