替换字段中的点和逗号,然后使用该值使用 jquery 自动填充另一个字段
我正在使用 Google 地图来计算城市之间的距离。我需要使用该距离值进行一些基本计算。距离必须采用“英美”格式 (1,234.00),但采用公制。因此,马德里 - 柏林查询的 Google 地图答案将是以下两个之一:
a) <span jscontent="distance['text']" jsdisplay="distance" jstcache="7">2.320,1 km</span>
b) <span jscontent="distance.text" jstcache="23">2.320,1 km</span>
请注意跨度“类”(jstcache 为 7 或 23)的差异以及缺少任何“id”或“名称”属性。
我想要完成的是:
1)将这些Google Maps距离值转换为“Angloamerican”格式(2,320公里)或(甚至更好)没有千位分隔符的格式,该格式仅使用点作为小数分隔符(2320.1公里)
2)使用过滤后的值值来填充名为距离的文本字段
我在整个网络上进行了搜索,但没有找到确切的答案。这篇文章是最有帮助的:
用表单提交上的文本字段的值填充隐藏的表单元素值(jQuery)
它对自动填充部分有一些帮助,但我不能让它与这个谷歌地图代码。这是我当前的代码:
<head>
...
<script type="text/javascript">
function submitMyForm(){
$('#distance').val($('jstcache^=7').val());
$('#Googlemap').submit();
}
</script>
...
</head>
<body>
...
<form id="myform">
<input type="text" id="distance" name="distance" value="" />
</form>
...
</body>
I'm using Google Maps to calculate distance between cities. I need to use that distance value for some basic calculations. Distance has to be in "Angloamerican" format (1,234.00) but in metric system. So, Google Maps answer for Madrid - Berlin query will be one of these two:
a) <span jscontent="distance['text']" jsdisplay="distance" jstcache="7">2.320,1 km</span>
b) <span jscontent="distance.text" jstcache="23">2.320,1 km</span>
Please notice the differences in span "classes" (jstcache is 7 or 23) and lack of any "id" or "name" attributes.
What I want to accomplish is:
1) Convert these Google Maps distance values to "Angloamerican" format (2,320 km) or (even better) format without thousands separator which would only use dots as decimal separator (2320.1 km)
2) Use that filtered value to populate a text field called distance
I searched all over the web, but didn't find the exact answer. This post was the most helpful:
Populate hidden form element value with the value of a text field on form submit (jQuery)
It helped me a bit with the auto-populate part, but I can't make it work in combination with this Google Maps code. Here is my current code:
<head>
...
<script type="text/javascript">
function submitMyForm(){
$('#distance').val($('jstcache^=7').val());
$('#Googlemap').submit();
}
</script>
...
</head>
<body>
...
<form id="myform">
<input type="text" id="distance" name="distance" value="" />
</form>
...
</body>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常,您可以使用 javascript string .replace() 函数来完成此操作。在这种情况下,这有点棘手,因为你正在做交换:句号换逗号,逗号换句号。这意味着您必须使用中间值。使用下划线的工作示例如下所示:
Generally, you can do this with the javascript string .replace() function. In this case, it's a little trickier because you're doing a swap: periods for commas, commas for periods. This means you have to use an intermediate value. A working example using underscores would be something like this:
获取价值:
本地化:
To grab the value:
To localize: