开发环境:Rails 4,Gem Simple_form
我制作了这个表单页面。
我想在2个表格填写地址下点击转换按钮到经度和纬度
(转换按钮单击 - >在两个表单下填充经度和纬度)
但点击转换按钮 - >加载此页面
有时这是有效的
查看档案
my_function
JS文件(隐藏我的apikey)
<div class="form-group">
<%= form.input :name, label: "상호명" %>
<%= form.input :address, label: "주소" %>
<button name="convert_address" type=button" id="convert_address">Convert</button>
<%= form.input :longitude, label: "경도" %>
<%= form.input :latitude, label: "위도" %>
<%= form.input :phone, label: "전화번호" %>
<div class="form-actions">
<%= form.button :submit %>
</div>
<% end %>
})
的application.js
$(document).ready(function() {
$.convertAddress = function (query, obj1, obj2, position) {
var url = "http://apis.daum.net/local/geo/addr2coord?apikey=xxxxxxxxxxxxx&q=" + encodeURI(query) + "&output=json";
$.getJSON(url + "&callback=?", function (data) {
$.each(data.channel.item, function (i, item) {
if (position == 1) {
obj1.val(item.point_y);
obj2.val(item.point_x);
} else if (position == 2) {
obj1.value = item.point_y;
obj2.value = item.point_x;
}
});
});
}
$("#menu-toggle").click(function (e) {
e.preventDefault();
$("#wrapper").toggleClass("active");
});
$("#convert_address").click(function (e) {
e.preventDefault();
console.log("test");
$.convertAddress($("#store_address").val(), $("#store_latitude"), $("#store_longitude"), 1);
});
控制器
//= require jquery
//= require jquery_ujs
//= require admin/menus_js.js
//= require admin/stores_js.js
//= require bootstrap
//= require bootstrap-sprockets
//= require turbolinks
//= require_tree .
rake routes
的routes.rb
def create
@store = Store.new(params.require(:store).permit(:name, :address, :phone, :longitude, :latitude))
if @store.save
redirect_to "/admin/stores"
else
render "/admin/stores/new"
end
end
我真的很抱歉,不会说英语很好...... 谢谢!