Rails: javascript addEventListener 没有找到动态形式的元素
Rails: javascript addEventListener does not find element in dynamic form
我正在尝试通过 AJAX 在 Rails 应用程序中实现动态表单,并做了几个教程,但没有成功。
资产文件夹中我的应用程序 js 文件的 Javascript 部分
var land = document.getElementById("cart_land_id");
land.addEventListener("change", function(){
Rails.ajax({
url: "/carts?land=" + land.value,
type: "GET"
})
})
对于视图中的此表单:
<%= form_for @cart, :url => {:action => "show_shipping"}, :html => {:method => "get"} do |f| %>
<%= f.select :land_id, options_for_select(@lands.map { |l| [l.name.titleize, l.id] }, {:id => 'lands_select', :prompt => "select a country"}) %><br>
<%= f.select :shippingservice_id, options_for_select([]) %><br>
<%= f.submit "Calculate shipping" %>
<% end %>
呈现为:
<form accept-charset="UTF-8" action="/carts/show_shipping/4" class="edit_cart" id="edit_cart_4" method="get">
<div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /></div>
<select id="cart_land_id" name="cart[land_id]"><option value="1">Afghanistan</option>
...
<select id="cart_shippingservice_id" name="cart[shippingservice_id]"></select><br>
<input name="commit" type="submit" value="Calculate shipping" />
</form>
产生一个
TypeError: null is not an object (evaluating 'land.addEventListener')
我的 Javascript 目录中有 jQuery。
为什么会发生这种情况,我该如何解决?
因为你有jQuery,你可以使用下面的代码
$("body").on("change", "#cart_land_id", function(event){
var landValue = $("#cart_land_id").val();
$.ajax({
url: "/carts?land=" + landValue,
type: "GET",
success: function(response) {
// handle response here
}
})
})
有两种添加监听器的方法:
为某些特定元素添加侦听器:document.getElementById("...").addEventListener(type, fn)
或 document.querySelector("...").addEventListener(type, fn)
。
使用匹配器向父元素添加侦听器:document.addEventListener(type, fn)
其中 fn
在继续之前检查 event.target.closest('#cart_land_id')
是否存在。
在你的情况下,jquery 会有所帮助,因为有 shorthand:
$(document).on("change", "#cart_land_id", function(event){
Rails.ajax({
url: "/carts?land=" + event.target.value,
type: "GET"
})
})
侦听器是在文档上设置的(没有其他元素可用于目标),但只有从 #cart_land_id
冒泡的事件才会发送到侦听器。
更新
要发出 ajax 请求,您绝对可以使用 fetch
- 大多数浏览器都原生支持它,并且可以进行 polyfill。
fetch("/carts?land=" + event.target.value).
then(function(resp) { return resp.text(); }).
then(function(text) {
// do something with text
// for instance replace the content of some element:
someEl.innerHTML = text;
});
有关详细信息,请参阅 https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch
我正在尝试通过 AJAX 在 Rails 应用程序中实现动态表单,并做了几个教程,但没有成功。
资产文件夹中我的应用程序 js 文件的 Javascript 部分
var land = document.getElementById("cart_land_id");
land.addEventListener("change", function(){
Rails.ajax({
url: "/carts?land=" + land.value,
type: "GET"
})
})
对于视图中的此表单:
<%= form_for @cart, :url => {:action => "show_shipping"}, :html => {:method => "get"} do |f| %>
<%= f.select :land_id, options_for_select(@lands.map { |l| [l.name.titleize, l.id] }, {:id => 'lands_select', :prompt => "select a country"}) %><br>
<%= f.select :shippingservice_id, options_for_select([]) %><br>
<%= f.submit "Calculate shipping" %>
<% end %>
呈现为:
<form accept-charset="UTF-8" action="/carts/show_shipping/4" class="edit_cart" id="edit_cart_4" method="get">
<div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="✓" /></div>
<select id="cart_land_id" name="cart[land_id]"><option value="1">Afghanistan</option>
...
<select id="cart_shippingservice_id" name="cart[shippingservice_id]"></select><br>
<input name="commit" type="submit" value="Calculate shipping" />
</form>
产生一个
TypeError: null is not an object (evaluating 'land.addEventListener')
我的 Javascript 目录中有 jQuery。
为什么会发生这种情况,我该如何解决?
因为你有jQuery,你可以使用下面的代码
$("body").on("change", "#cart_land_id", function(event){
var landValue = $("#cart_land_id").val();
$.ajax({
url: "/carts?land=" + landValue,
type: "GET",
success: function(response) {
// handle response here
}
})
})
有两种添加监听器的方法:
为某些特定元素添加侦听器:
document.getElementById("...").addEventListener(type, fn)
或document.querySelector("...").addEventListener(type, fn)
。使用匹配器向父元素添加侦听器:
document.addEventListener(type, fn)
其中fn
在继续之前检查event.target.closest('#cart_land_id')
是否存在。
在你的情况下,jquery 会有所帮助,因为有 shorthand:
$(document).on("change", "#cart_land_id", function(event){
Rails.ajax({
url: "/carts?land=" + event.target.value,
type: "GET"
})
})
侦听器是在文档上设置的(没有其他元素可用于目标),但只有从 #cart_land_id
冒泡的事件才会发送到侦听器。
更新
要发出 ajax 请求,您绝对可以使用 fetch
- 大多数浏览器都原生支持它,并且可以进行 polyfill。
fetch("/carts?land=" + event.target.value).
then(function(resp) { return resp.text(); }).
then(function(text) {
// do something with text
// for instance replace the content of some element:
someEl.innerHTML = text;
});
有关详细信息,请参阅 https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch