admin管理员组文章数量:1332383
I want to restrict the input in multiple forms and I'm trying to remove letters from the input on the keyup event. The problem is that It seems to work only on type="text"
inputs, but I need it on type="number"
inputs.
Fiddle.
NOTE: I need this working at Firefox and IE. On chrome this functionality isn't neccesary.
$(function(){
$('input[type="number"], [type="text"]').keyup(function(e) {
if(this.value!='-')
while(isNaN(this.value))
this.value = this.value.split('').reverse().join('').replace(/[\D]/i,'')
.split('').reverse().join('');
})
.on("cut copy paste",function(e){
e.preventDefault();
});
});
<script src=".9.1/jquery.min.js"></script>
<input type="number">
<input type="text">
I want to restrict the input in multiple forms and I'm trying to remove letters from the input on the keyup event. The problem is that It seems to work only on type="text"
inputs, but I need it on type="number"
inputs.
Fiddle.
NOTE: I need this working at Firefox and IE. On chrome this functionality isn't neccesary.
$(function(){
$('input[type="number"], [type="text"]').keyup(function(e) {
if(this.value!='-')
while(isNaN(this.value))
this.value = this.value.split('').reverse().join('').replace(/[\D]/i,'')
.split('').reverse().join('');
})
.on("cut copy paste",function(e){
e.preventDefault();
});
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="number">
<input type="text">
Share
Improve this question
edited May 23, 2017 at 9:35
IXTR Unai
asked May 23, 2017 at 9:09
IXTR UnaiIXTR Unai
3774 silver badges17 bronze badges
9
-
2
Remove the space?
input [type
->input[type
– evolutionxbox Commented May 23, 2017 at 9:10 - 4 which browser you are using to test? in chrome it works. – Dinesh undefined Commented May 23, 2017 at 9:10
-
1
remove space. Use
input[type="number"], input[type="text"]
– Tushar Commented May 23, 2017 at 9:11 - 1 I just edited, chrome doesn´t allow you to put text on number type inputs. I'm testing this at Firefox and IE11. – IXTR Unai Commented May 23, 2017 at 9:12
- change your selector $('input[type="number"],input[type="text"]') – JYoThI Commented May 23, 2017 at 9:15
5 Answers
Reset to default 5You shouldn't have a space between the element selector and the attribute selector.
input [type="number"]
Should be:
input[type="number"]
The [type="text"]
happens to work because it matches all elements with a type attribute that is set as text
. It really should also be:
input[type="text"]
And the whole selector:
'input[type="number"],input[type="text"]'
There are also issues with the function - the two split('').reverse().join('')
? Not needed.
A better approach for this function would be:
$('input[type="number"], input[type="text"]').keyup(function(e) {
this.value = this.value.replace(/[^0-9-]/i, '');
})
Remove Space in between element(input) and attribute([type="text"])
$(document).ready(function(){
$("input[type='text'],input[type='number']").keydown(function(){
$("input[type='text']").css("background-color", "yellow");
});
$("input[type='text'],input[type='number']").keyup(function(){
$("input[type='number']").css("background-color", "pink");
});
});
</script>
The bug was happening because the way IE11 populates the value
attribute is strange for type="number".
When I type '1a'
then then this.value
is '1a'
, so your code executes correctly.
When I type 'a1'
then then this.value
is ''
, so isNaN('')
bees false and your code doesn't execute.
I don't why IE11 is so strange.
I would remend not using the isNaN
and generally improving your coding standards. There were many minor issues already noted in ments.
Here is an example which works in IE11. (This still has some issues testing in Chrome, but you already stated you are not interest in Chrome)
$(function(){
$('[type="number"], [type="text"]').keyup(function() {
this.value = this.value.replace(/\D/g, '');
})
.on("cut copy paste",function(e){
e.preventDefault();
});
});
<script src="https://ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="number">
<input type="text">
You can try this following link,
https://jsfiddle/tkfnxpvy/4/
use
$('input[type="number"],input[type="text"]'.keyup(function(e){
});
Okay guys, thanks for all the help, I think I finally solved here.
JSFiddle
<script src="https://ajax.googleapis./ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<input type="number" oninput="this.value = this.value.replace(/[^0-9.,]/g, '');">
<input type="text" oninput="this.value = this.value.replace(/[^0-9.,]/g, '')">
本文标签: javascriptInput typequotnumberquot selector not workingStack Overflow
版权声明:本文标题:javascript - Input type="number" selector not working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742294486a2448446.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论