jQuery点击不注册哪个播放器点击。我缺少什么? [英] jQuery click not registering which player is clicking. What am I missing?

查看:85
本文介绍了jQuery点击不注册哪个播放器点击。我缺少什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上一个问题/答案: jQuery .click函数不是工作没有字符串

我试图通过RoR / jQuery / HTML构建一个井字游戏,我不知道为什么玩家没有在点击上注册。以前,我无法得到 .val 工作,因为 currentPlayer 不是一个字符串。但现在是,只有它不是显示为两个选择之一。不确定代码出错的位置。

I am trying to build a tic-tac-toe game through RoR/jQuery/HTML and I can't figure out why the players aren't being registered on the clicks. Previously I was unable to get the .val to work at all because currentPlayer wasn't a string. But now it is, only it is not showing up as either of the two choices. Not sure where the code is wrong.

jQuery:

$(document).ready(function(){

  var currentPlayer = $("#table").data("current-player");

  $(".square").click(function(){
    //   Gather the position that was clicked
    var number = $(this).data("position");
    // Locate the game form
    var form = $("form");
    // Locate the input field corresponding to that position
    var input = $("input[data-position='" + number + "']");
    // Set the value of that input field to "X" or "O"
    input.val(currentPlayer);
    // Submit the form
    form.submit();
  });
});

Ruby:

def update
  @game = Game.find(params[:id])
  @game.update(game_params)
  redirect_to @game
  switch_player
end

def switch_player
  session[:current_player] = session[:current_player] == 'X' ? 'O' : 'X'
end

HTML表单:

<%= nested_form_for @game do |f| %>

  <%= f.fields_for :moves do |move_form| %>
    <p id="table" data-current-player: <%=session[:current_player] %>>
      <%= move_form.label :position %><br>
      <%= move_form.text_field :player, data: {position: move_form.object.position} %>
      <%= move_form.hidden_field :id %>
    </p>
  <% end %>

<input type="Submit">
<% end %>

HTML表格(仅限第一个储存格):

HTML table (first cell only):

<div id="board" align = center>
  <table>
    <tr>
      <td data-position="0" class="square <%= class_for_move(0)%>"></td>

Ruby class_for_move:

Ruby class_for_move:

def class_for_move(number)
  move = @game.moves.find_by(position: number)
  player = move.player
  player.downcase + '_move' unless player.blank?
end



我觉得,给定的是什么,应该是工作。但是当我运行页面,并单击一个单元格,它提交没有内容。它应该是< td data-position =0class =square>< / td> ; td data-position =0class =square x_move>< / td> 或o_move。

I feel like, given what is there, it should be working. But when I run the page, and click on a cell, it submits with nothing inside. It comes out as <td data-position="0" class="square "></td> when it should be <td data-position="0" class="square x_move"></td>, or o_move.

console.log currentPlayer ,并显示为未定义。

I used console.log on currentPlayer and it is showing up as undefined.

推荐答案

它可能与这种语法有关: data-current-player:<%= session [:current_player]%& code>应该是 data-current-player ='<%= session [:current_player]%>'

Could it have something to do with this syntax: data-current-player: <%=session[:current_player] %> where it should be data-current-player='<%=session[:current_player] %>'?

这篇关于jQuery点击不注册哪个播放器点击。我缺少什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆