如何使用 Jquery 添加输入字段的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1221239/
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 add to value of an input field with Jquery
提问by
I have a input field like below,
我有一个如下所示的输入字段,
<input name="update_price" type="text" id="update_price" value="£" size="8"
maxlength="8" />
I'd like to get some price value from the value of a select box and add it after the £ sign, how is this achieved in jquery,
我想从选择框的值中获取一些价格值并将其添加到 £ 符号之后,这是如何在 jquery 中实现的,
<form>
<select name="select" id="select">
<option>Select your pizza</option>
<option value="6.65">NY, 10", £6.65</option>
<option value="8.95">NY, 12", £8.95</option>
<option value="11.95">NY, 16", £11.95</option>
<option value="3.45">Chicago, 7", £3.45</option>
<option value="6.65">Chicago, 10", £6.65</option>
<option value="8.95">Chicago, 12", £8.95</option>
<option value="11.95">Chicago, 16", £11.95</option>
<option value="19.95">Chicago, Beast 24" x 18", £19.95</option>
</select>
</form>
$(function()
{
$('#select').change( function() {
$('input[name=update_price]').val($("#select :selected").val() );
});
});
it works with the code above but the £ sign disappears, any help would be appreciated.
它适用于上面的代码,但 £ 符号消失,任何帮助将不胜感激。
采纳答案by Al.
In the past I have used a background image on the input box for the currency symbol. That way the symbol can look pretty, be left aligned (while the value is right aligned) and it doesn't get in the way of calculations.
过去,我在货币符号的输入框中使用了背景图像。这样符号看起来很漂亮,左对齐(而值右对齐)并且不会妨碍计算。
Hope that helps
希望有帮助
回答by Emre Erkan
Are the encoding of file and page is same? If you want to use special characters like pound sign you have to save your javascript file UTF-8 (without bom character. You can use notepad++ or a similar editor for that)
文件和页面的编码是否相同?如果你想使用像井号这样的特殊字符,你必须保存你的 javascript 文件 UTF-8(没有 bom 字符。你可以使用 notepad++ 或类似的编辑器)
Btw you specified an id for input, so why not using that for assigning it's value and last, you can use thiswhen you are in the change function scope. See below
顺便说一句,你指定输入自己的ID,那么为什么不使用分配它的价值,最后,你可以用这个当你在改变功能范围。见下文
$(function() {
$('#select').change(function() {
$('#update_price').val("£" + $(this).val());
});
});
回答by James Wiseman
To get the pound sign try
要获得英镑符号,请尝试
£ or £
£ 或&磅;
also, check out this for more on these codes: http://www.ascii.cl/htmlcodes.htm
另外,请查看有关这些代码的更多信息:http: //www.ascii.cl/htmlcodes.htm
回答by blub
One solution is to put the Pound sign in the value tag of the options in the select box.
一种解决方案是将英镑符号放在选择框中选项的值标签中。
<option value=" 6.65">NY, 10", £6.65</option>
But that might deliver problems when calculating with this value. Nicer is the following:
但是,在使用此值进行计算时,这可能会带来问题。更好的是以下内容:
$('input[name=update_price]').val(""+$("#select :selected").val() );
I don't have the pound sign on my keyboard, hence the :)
我的键盘上没有英镑符号,因此:)
//edit
//编辑
<input type='hidden' name='shopping_cart_value' value='0'>
$('input[name=update_price]').val(""+$("#select :selected").val() );
$('input[name=shopping_cart_value]').val($("#select :selected").val() );