HTML how to set value of <input type=“number”> to infinity

旧城冷巷雨未停 提交于 2021-01-27 05:06:18

问题


I have a number input that I want to hold integers. If the value gets set to negative then I want the symbol shown to be ∞ (&#8734;).

Doing this doesn't work (a blank is shown instead of "∞"):

<%
if foo <= -1
    v = '&#8734;'
else
    v = foo
end
%>
<input type="number" value="<%= v %>">

Is there a way to do it without resorting to javascript?


回答1:


What are your requirements for having a "number" field? Can you rely on form validation and use Javascript to progressively enhance just typing numbers. Then use something like this

<input type="text" pattern="([0-9]|&#8734;)+">

(pattern will prevent the form from submitting unless it's a number or infinity symbol)




回答2:


You need to use type text for this field and convert infinity sign using CGI::unescapeHTML

<% 
   if foo <= -1
     v = CGI::unescapeHTML('&#8734;')
   else
     v = foo
   end
%>
<input type="text" value="<%= v %>">


来源:https://stackoverflow.com/questions/22844728/html-how-to-set-value-of-input-type-number-to-infinity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!