在输入中传递值
Passing value in input
本文关键字:输入 更新时间:2023-09-26
我使用Yandex API计算从a点到B点的旅行距离和价格。在我的示例中,您可以通过在地图的不同部分单击2次来尝试自己:http://jsfiddle.net/EveGeen/wor9afo0/
ymaps.route([start, finish]).then(function (router) {
var distance = Math.round(router.getLength() / 1000),
message = '<span>Distance: ' + distance + 'km.</span><br/>' +
'<span style="font-weight: bold; font-style: italic">Price: %sр.</span>';
self._route = router.getPaths();
self._route.options.set({ strokeWidth: 5, strokeColor: '0000ffff', opacity: 0.5 });
self._map.geoObjects.add(self._route);
self._start.properties.set('balloonContentBody', startBalloon + message.replace('%s', self.calculate(distance)));
self._finish.properties.set('balloonContentBody', finishBalloon + message.replace('%s', self.calculate(distance)));
});
你会看到,在设置了A、B之后,你可以按下这些字母中的任何一个,它会显示距离和价格。
我如何将这两个值(距离和价格,第127-128行)传递到顶部的两个输入中?我只需要数字而不需要文字。
向输入元素添加id:
<label>Distance:</label>
<input id="dist" type="text" size="40" name="dist">
<br>
<label>Price:</label>
<input id="price" type="text" size="40" name="price">
并将其添加到route
事件中,以这种方式设置其值:
document.getElementById("dist").value = distance; // distance here
document.getElementById("price").value = self.calculate(distance);// price here
您需要设置route
事件中输入元素的值:
ymaps.route([start, finish])
.then(function (router) {
var distance = Math.round(router.getLength() / 1000),
message = '<span>Distance: ' + distance + 'km.</span><br/>' +
'<span style="font-weight: bold; font-style: italic">Price: %sр.</span>';
self._route = router.getPaths();
self._route.options.set({
strokeWidth: 5,
strokeColor: '0000ffff',
opacity: 0.5
});
self._map.geoObjects.add(self._route);
self._start.properties.set('balloonContentBody', startBalloon + message.replace('%s', self.calculate(distance)));
self._finish.properties.set('balloonContentBody', finishBalloon + message.replace('%s', self.calculate(distance)));
document.getElementById("dist").value = distance; // distance here
document.getElementById("price").value = self.calculate(distance);// price here
});
工作演示:
更新的FIDDLE
document.querySelectorAll("input[name='dist']")[0].value = distance; // distance
document.querySelectorAll("input[name='price']")[0].value = self.calculate(distance) // price
相关文章:
- 为什么不't Javascript对我的输入值进行了一些重新检查
- 可以't让我的if语句处理js中的html表单输入
- 名称输入的索引
- 如何编写HTML输入的JS内联
- 要求输入在数据列表中
- 将输入字段中的文本提交到我的数据库,同时将其添加到我的列表中
- 让文本输入幻灯片显示输入时的新文本输入?然后向后滑动
- 如何将输入(type=text)从html表单传递到javascript函数
- 单击jquery清除输入值
- 而循环只设置php中输入字段中的第一个值
- 在输入字段中将最小金额设置为
- jQuery自定义验证比较多个输入的序列
- Sails.js:同时发布文本输入和一个文件
- 使用javascript检查多个输入值,并在1次检查中标记多个输入框
- 如何在输入字段中的按钮的帮助下打开日历,该字段的类型为“=”;日期”;
- 使用jquery在单击时在单元格中输入值
- Ajax文件加载和<输入>文件加载
- 是否可以将一个函数输入连接到另一个函数调用的文本
- 将值输入到对象,然后该对象推送到数组
- 如果我在javascript中输入无效的电子邮件或空白,如何显示特定的文本框边框红色