Javascript RubyonRails/HTML表

Javascript RubyonRails/HTML表,javascript,html,ruby-on-rails,ruby,checkbox,Javascript,Html,Ruby On Rails,Ruby,Checkbox,这是编辑后的代码: 我尝试用“名称”代替“纬度”,但仍然得到一个空白的警报窗口。 我还在app/views/layouts/application.html.erb中的中添加了 <% @clients.each do |client| %> <tr class = "client"> <td class = "name"><%= client.name %></td>

这是编辑后的代码: 我尝试用“名称”代替“纬度”,但仍然得到一个空白的警报窗口。 我还在app/views/layouts/application.html.erb中的
中添加了

    <% @clients.each do |client| %>
          <tr class = "client">
            <td class = "name"><%= client.name %></td>
            <td class = "phone"><%= client.phone %></td>
            <td class = "address"><%= client.address %></td>
            <td class = "pay"><%= client.pay %></td>
            <td class = "latitude"><%= client.latitude %></td>
            <td class = "longitude"><%= client.longitude %></td>
            <td><form><input class = "route" type="checkbox" name = "chkboxRoute"></form></td>
          </tr>
        <% end %>

<script>
$(function() {
  $(".client input.route").bind("change", function(element) {
    var clatitude = [];
    var clongitude = [];

    $(".client input.route:checked").each(function(element) {
      element = $(element);

      var clientLatitude = $(element).closest('.client').find('.latitude');
      clatitude.push(clientLatitude.text());

      var clientLongitude = $(element).closest('.client').find('.longitude');
      clongitude.push(clientLongitude.text());
    });
        window.alert(clatitude[0]);
  })
});
</script>

$(函数(){
$(“.client input.route”).bind(“change”,函数(元素){
变量clatitude=[];
var clongitude=[];
$(“.client input.route:checked”)。每个(函数(元素){
元素=$(元素);
var clientLatitude=$(元素)。最近('.client')。查找('.latitude');
clatitude.push(clientLatitude.text());
var clientLongitude=$(元素)。最近('.client')。查找('.longitude');
clongitude.push(clientLongitude.text());
});
窗口警报(clatitude[0]);
})
});
您应该包括帮助您解决问题的方法。在
layout.html.erb
文件中,将其添加到
中:

还有Javascript:

// $ is the jQuery global object
// Passing it a function will wait to execute that code until
// the entire HTML document is ready
$(function() {
  // Select every input with the "route" class that is checked
  var names = [];
  $(".client input.route:checked").each(function(index, element) {
    // Wrap the element in jQuery so we get access to all of its functions
    element = $(element);

    // Find the closest element with class "client",
    // then find an element nested inside of it with class "name"
    var clientName = $(element).closest('.client').find('.name');

    // Add the text from that element to the array
    names.push(clientName.text());
  });

  // At this point, names will hold all of the names of the clients
  // with a checked route input
});
请注意,这将仅在页面加载时运行。如果您希望它在这些更改中的任何一次运行,您可以将函数绑定到
change
事件:

$(function() {
  $(".client input.route:checked").bind("change", function(element) {
    var names = [];
    $(".client input.route:checked").each(function(element) {
      element = $(element);
      var clientName = $(element).closest('.client').find('.name');
      names.push(clientName.text());
    });
  })
});
每次更改复选框时,这将贯穿所有复选框。这不是最好的性能,但这应该让您开始。

您应该包括帮助您解决问题的。在
layout.html.erb
文件中,将其添加到
中:

还有Javascript:

// $ is the jQuery global object
// Passing it a function will wait to execute that code until
// the entire HTML document is ready
$(function() {
  // Select every input with the "route" class that is checked
  var names = [];
  $(".client input.route:checked").each(function(index, element) {
    // Wrap the element in jQuery so we get access to all of its functions
    element = $(element);

    // Find the closest element with class "client",
    // then find an element nested inside of it with class "name"
    var clientName = $(element).closest('.client').find('.name');

    // Add the text from that element to the array
    names.push(clientName.text());
  });

  // At this point, names will hold all of the names of the clients
  // with a checked route input
});
请注意,这将仅在页面加载时运行。如果您希望它在这些更改中的任何一次运行,您可以将函数绑定到
change
事件:

$(function() {
  $(".client input.route:checked").bind("change", function(element) {
    var names = [];
    $(".client input.route:checked").each(function(element) {
      element = $(element);
      var clientName = $(element).closest('.client').find('.name');
      names.push(clientName.text());
    });
  })
});
每次更改复选框时,这将贯穿所有复选框。这不是最好的性能,但这应该让您开始。

您应该包括帮助您解决问题的。在
layout.html.erb
文件中,将其添加到
中:

还有Javascript:

// $ is the jQuery global object
// Passing it a function will wait to execute that code until
// the entire HTML document is ready
$(function() {
  // Select every input with the "route" class that is checked
  var names = [];
  $(".client input.route:checked").each(function(index, element) {
    // Wrap the element in jQuery so we get access to all of its functions
    element = $(element);

    // Find the closest element with class "client",
    // then find an element nested inside of it with class "name"
    var clientName = $(element).closest('.client').find('.name');

    // Add the text from that element to the array
    names.push(clientName.text());
  });

  // At this point, names will hold all of the names of the clients
  // with a checked route input
});
请注意,这将仅在页面加载时运行。如果您希望它在这些更改中的任何一次运行,您可以将函数绑定到
change
事件:

$(function() {
  $(".client input.route:checked").bind("change", function(element) {
    var names = [];
    $(".client input.route:checked").each(function(element) {
      element = $(element);
      var clientName = $(element).closest('.client').find('.name');
      names.push(clientName.text());
    });
  })
});
每次更改复选框时,这将贯穿所有复选框。这不是最好的性能,但这应该让您开始。

您应该包括帮助您解决问题的。在
layout.html.erb
文件中,将其添加到
中:

还有Javascript:

// $ is the jQuery global object
// Passing it a function will wait to execute that code until
// the entire HTML document is ready
$(function() {
  // Select every input with the "route" class that is checked
  var names = [];
  $(".client input.route:checked").each(function(index, element) {
    // Wrap the element in jQuery so we get access to all of its functions
    element = $(element);

    // Find the closest element with class "client",
    // then find an element nested inside of it with class "name"
    var clientName = $(element).closest('.client').find('.name');

    // Add the text from that element to the array
    names.push(clientName.text());
  });

  // At this point, names will hold all of the names of the clients
  // with a checked route input
});
请注意,这将仅在页面加载时运行。如果您希望它在这些更改中的任何一次运行,您可以将函数绑定到
change
事件:

$(function() {
  $(".client input.route:checked").bind("change", function(element) {
    var names = [];
    $(".client input.route:checked").each(function(element) {
      element = $(element);
      var clientName = $(element).closest('.client').find('.name');
      names.push(clientName.text());
    });
  })
});

每次更改复选框时,这将贯穿所有复选框。这不是最好的性能,但这应该让您开始了。

在复选框中添加一些数据-您的client.id(如果没有其他数据),以某种方式。这会让你的生活更轻松。您可以通过两种方式实现这一点,但最简单的是作为名称的一部分:

<input type="checkbox" name = "chkboxRoute_<%= client.id %>">
我只是喜欢这样做,因为它不太依赖html的结构

现在,如果你真的想获得金牌,我会这样做:

<% @clients.each do |client| %>
  <tr class="client">
    <td><%= client.name %></td>
    <td><%= client.phone %></td>
    <td><%= client.address %></td>
    <td><%= client.pay %></td>
    <td><%= client.latitude %></td>
    <td><%= client.longitude %></td>
    <td><input class="route" type="checkbox" name = "chkboxRoute" value="<%= client.id %>"></td>
  </tr>
<% end %>

<script type="text/javascript">
  var clients = [];
  <% @clients.each do |client| %>
    clients[<%= client.id %>] = {
      name: "<%= j(client.name) %>",
      phone: "<%= j(client.phone) %>",
      ....
    };
  <% end %>
</script>

如果愿意,您甚至可以使用json模板将数组组合在一起。无论如何,这应该给你足够的时间去思考。

以某种方式在复选框中添加一些数据-你的client.id(如果没有其他内容)。这会让你的生活更轻松。您可以通过两种方式实现这一点,但最简单的是作为名称的一部分:

<input type="checkbox" name = "chkboxRoute_<%= client.id %>">
我只是喜欢这样做,因为它不太依赖html的结构

现在,如果你真的想获得金牌,我会这样做:

<% @clients.each do |client| %>
  <tr class="client">
    <td><%= client.name %></td>
    <td><%= client.phone %></td>
    <td><%= client.address %></td>
    <td><%= client.pay %></td>
    <td><%= client.latitude %></td>
    <td><%= client.longitude %></td>
    <td><input class="route" type="checkbox" name = "chkboxRoute" value="<%= client.id %>"></td>
  </tr>
<% end %>

<script type="text/javascript">
  var clients = [];
  <% @clients.each do |client| %>
    clients[<%= client.id %>] = {
      name: "<%= j(client.name) %>",
      phone: "<%= j(client.phone) %>",
      ....
    };
  <% end %>
</script>

如果愿意,您甚至可以使用json模板将数组组合在一起。无论如何,这应该给你足够的时间去思考。

以某种方式在复选框中添加一些数据-你的client.id(如果没有其他内容)。这会让你的生活更轻松。您可以通过两种方式实现这一点,但最简单的是作为名称的一部分:

<input type="checkbox" name = "chkboxRoute_<%= client.id %>">
我只是喜欢这样做,因为它不太依赖html的结构

现在,如果你真的想获得金牌,我会这样做:

<% @clients.each do |client| %>
  <tr class="client">
    <td><%= client.name %></td>
    <td><%= client.phone %></td>
    <td><%= client.address %></td>
    <td><%= client.pay %></td>
    <td><%= client.latitude %></td>
    <td><%= client.longitude %></td>
    <td><input class="route" type="checkbox" name = "chkboxRoute" value="<%= client.id %>"></td>
  </tr>
<% end %>

<script type="text/javascript">
  var clients = [];
  <% @clients.each do |client| %>
    clients[<%= client.id %>] = {
      name: "<%= j(client.name) %>",
      phone: "<%= j(client.phone) %>",
      ....
    };
  <% end %>
</script>

如果愿意,您甚至可以使用json模板将数组组合在一起。无论如何,这应该给你足够的时间去思考。

以某种方式在复选框中添加一些数据-你的client.id(如果没有其他内容)。这会让你的生活更轻松。您可以通过两种方式实现这一点,但最简单的是作为名称的一部分:

<input type="checkbox" name = "chkboxRoute_<%= client.id %>">
我只是喜欢这样做,因为它不太依赖html的结构

现在,如果你真的想获得金牌,我会这样做:

<% @clients.each do |client| %>
  <tr class="client">
    <td><%= client.name %></td>
    <td><%= client.phone %></td>
    <td><%= client.address %></td>
    <td><%= client.pay %></td>
    <td><%= client.latitude %></td>
    <td><%= client.longitude %></td>
    <td><input class="route" type="checkbox" name = "chkboxRoute" value="<%= client.id %>"></td>
  </tr>
<% end %>

<script type="text/javascript">
  var clients = [];
  <% @clients.each do |client| %>
    clients[<%= client.id %>] = {
      name: "<%= j(client.name) %>",
      phone: "<%= j(client.phone) %>",
      ....
    };
  <% end %>
</script>

如果愿意,您甚至可以使用json模板将数组组合在一起。不管怎样,这应该给你足够的时间去思考。

你的
不是倒着走吗?您有jQuery可用吗?您的
不是向后吗?您有jQuery可用吗?您的
不是向后吗?您有jQuery可用吗?您的
不是向后吗?你有可用的jQuery吗?我可能会使用
$(element).最近的('.client').find('.name')
来减少对特定HTML的依赖性,但这只是吹毛求疵。我想我已经设法让它工作了,但我想通过在第一个}之后添加一个窗口来测试它;但我得到的只是一扇空白的窗户。非常感谢您的帮助。您能在
element=$(element)
行后面添加
console.log(element.closest(“.client”).html())
吗?或者用你的HTMLI更新你的问题。我可能会使用
$(element).最近的('.client')。查找('.name')
对特定HTML的依赖性要小一些,但这只是吹毛求疵。我想我已经设法让它工作了,但我想通过在第一个}之后添加一个窗口来测试它;但我得到的只是一扇空白的窗户。我真的很感谢你的帮助