javascript 如何通过javascript在html输入字段中放置度数符号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31669536/
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
How to put a degree symbol in an html input field through javascript
提问by BiJ
I have a text box as
我有一个文本框作为
HTML
HTML
<input type="text" id="lat" placeholder="Latitude" ng-lat value="9°">
which works fine as it displays the degree sign perfectly, but when I try to set the value through jquery like
它工作正常,因为它完美地显示了度数符号,但是当我尝试通过 jquery 设置值时
Jquery
查询
$("#lat").val("9°")
$("#lat").val("9°")
Its displaying the entire text instead of the degree symbol.
它显示整个文本而不是度数符号。
回答by Pindo
You can target the value
attribute:
您可以定位value
属性:
$("#lat").attr('value',"101"+String.fromCharCode(176))
it is also better to use .text()
to set the text because .val()
is used to retrieve the value of an element
.text()
用于设置文本也更好,因为.val()
用于检索元素的值
回答by Ayush
$("#test").click(function(){
$("#lat").val("29" + ascii(176))
});
function ascii (a) { return String.fromCharCode(a); }
Updated JSFiddle for further help: http://jsfiddle.net/zkh2of12/1/
更新了 JSFiddle 以获得进一步帮助:http: //jsfiddle.net/zkh2of12/1/
回答by Parag Jadhav
回答by Muhammad Atif
Try this:
试试这个:
$("#lat").attr("val","9"+ascii(176));
回答by Eduardo Yá?ez Parareda
Use the special tag besides the input field, it will be clear for the user:
使用除输入字段外的特殊标签,用户将一目了然:
<input type="text" id="lat" placeholder="Latitude °" value=""> °
回答by smnbbrv
What about just setting the symbol directly in JavaScript instead of the HTML code?
直接在 JavaScript 中而不是在 HTML 代码中设置符号怎么样?
$(function(){
$("#set").click(function(){
$("#lat").val("°");
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="lat" placeholder="Latitude" ng-lat value="9°">
<button id="set">set</button>
If you want to improve those just make a hidden <span>
with the html code you like inside and then just pick the html with JavaScript. This will work even for symbols you cannot post in JS
如果你想改进这些,只需<span>
在里面隐藏你喜欢的 html 代码,然后用 JavaScript 选择 html。这甚至适用于您无法在 JS 中发布的符号