javascript - 使用Rails Simple Form gem时如何通过JS onclick更改输入字段值
问题描述
我在我的 Rails 应用程序中设置了一个 JS 函数,用于获取用户的当前位置。我希望通过单击按钮将返回的坐标输入到我的 Rails 简单表单中的字段中。我尝试使用innerHTML
andvalue
来更改元素,但这不适用于简单的表单 gem。任何想法让他的工作?
这是JS代码:
var x = document.getElementById("coordinates");
function getLocation() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(showPosition);
} else {
x.innerHTML = "Geolocation is not supported by this browser.";
}
}
function showPosition(position) {
x.value = position.coords.latitude +
", " + position.coords.longitude;
}
还有我的简单表单视图:
<button onclick="getLocation()">Current location</button>
<input id="coordinates"></input>
<%= simple_form_for @order do |f| %>
<%= f.input :start_point, input_html: { id: 'coordinates' } %>
<%= f.input :restaurant_location %>
<%= f.input :customer_location %>
<%= f.input :fee %>
<%= f.button :submit %>
<% end %>
解决方案
推荐阅读
- fluentd - fluentd mongo_tail 不会将集合记录到标准输出
- python - BeautifulSoup 抓取.text 属性问题
- ninja - 使用 X11 (c++) 为 lib 链接目标失败
- typescript - 在赋值时访问对象属性键
- python - two component gaussian fit not working correctly
- c# - Asynchronous socket problem reading byte[] in server
- node.js - when running npm install I am getting back npm ERR! code E404
- python - 在基于 Python 的应用程序中从 Kubernetes 读取机密
- c# - Dynamics 365 - 具有不存在条件的 Linq 查询
- java - Azure (Java) BlobClient.uploadWithResponse 何时准备好上传?