Html 弹簧形式:输入数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27141458/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:07:06  来源:igfitidea点击:

Spring form:input for number

htmljspjsp-tagsspring-form

提问by iAmLearning

I am using Spring's form:input as below :

我正在使用 Spring 的 form:input 如下:

<form:input type="number" .....>

in my jsp but when I check the html that is rendered on the browser it shows like :

在我的 jsp 中,但是当我检查浏览器上呈现的 html 时,它显示如下:

type="number" type="text"

i.e., two typeattributes are generated in the html.

type在html中生成了两个属性。

On the other hand, if I check using inspect element optionin the browser, it shows correct - only type="number"as expected.
Edit- My Question: Why am I getting two type attributes in generated html (type="number" type="text") ? How to get it resolved?

另一方面,如果我在浏览器中使用检查元素选项进行检查,它会显示正确 - 仅type="number"按预期进行。
编辑- 我的问题:为什么我在生成的 html ( type="number" type="text") 中得到两个类型属性?如何解决?

采纳答案by Santhosh

Spring form:inputtag doesnt have any attribute named typeand the type=numberused in your code belongs to html5input tag

Springform:input标签没有任何命名type的属性,并且type=number在您的代码中使用的属于html5输入标签

Also have a look at HTML Text Input allow only Numeric input

也看看HTML 文本输入只允许数字输入

Spring form tld lists the valid attributes of form:inputelement here

Spring 表单 tld 在这里列出了form:input元素的有效属性

回答by tk_

HTML 5with <!DOCTYPE html>has native solution:

HTML 5<!DOCTYPE html>拥有本土的解决方案

<input type="number">

<input type="number">

Beware that it does not behave in standard way in some browsers.

请注意,它在某些浏览器中的行为不符合标准

Try input type="number"to see the HTML5 version in action.

尝试输入 type="number"以查看正在运行的 HTML5 版本。

See also https://github.com/jonstipe/number-polyfillfor transparent support in older browsers.

另请参阅https://github.com/jonstipe/number-polyfill以了解旧浏览器中的透明支持。

回答by Ben Vae

I know this is old, but i found a solution that works just fine for me.

我知道这很旧,但我找到了一个对我来说很好用的解决方案。

The spring:inputform tag doesnt support the typeattribute and there is no such thing as spring:number. But you can use jquery to add the type="number"attribute to the parsed html.

spring:input表单标签犯规支持type的属性并没有这样的事情spring:number。但是您可以使用 jquery 将type="number"属性添加到解析的 html 中。

I used:

我用了:

$(".selector").attr({
    "type" : "number",
});