Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 表在AJAX函数运行后消失_Javascript_Jquery_Ruby On Rails_Ajax - Fatal编程技术网

Javascript 表在AJAX函数运行后消失

Javascript 表在AJAX函数运行后消失,javascript,jquery,ruby-on-rails,ajax,Javascript,Jquery,Ruby On Rails,Ajax,出于某种原因,单击从控制器请求数据的“我的”按钮后,我的表消失了。这是我的桌子: <div id="SelectedTm" style= float:right> <table id = "PlyrsTm2" style = float:right> <tr><th id="PTTitle" colspan=2>List of players on selected team</th></tr>

出于某种原因,单击从控制器请求数据的“我的”按钮后,我的表消失了。这是我的桌子:

    <div id="SelectedTm" style= float:right>
    <table id = "PlyrsTm2" style = float:right>
        <tr><th id="PTTitle" colspan=2>List of players on selected team</th></tr>
        <tr><th id="PTHRows">Player</th><th id="PTHRows">Team</th></tr>
        <% @pl.each do |f| %>
            <tr><td class="player"><%= f.Plyr %></td><td class="team"><%= f.Team%></td></tr>
        <% end %>
    </table>
</div>
现在您可以看到,我的表由rails填充。每次我选择一个新团队时,表格就会消失。只有重新加载页面时才会重新显示,但这是最初选择的团队,默认情况下是第一个团队

更新 这是我的控制器:

class TeamplayersController < ApplicationController
    before_filter :set_id
    before_action :set_id, :set_teamplayer, only: [:show, :edit, :update, :destroy]

# GET /teamplayers
# GET /teamplayers.json
def index
  @teamplayers = Teamplayer.all
  @fteams = Fteam.all
  tid = params[:resolution]
  toimport = params[:import]

puts tid
  if tid.nil? == true
    tid = 1
        @ids = tid
        @pl =  Teamplayer.joins(:live_player).where(:teamid => @ids).all
  else
    tid = tid.to_i;
        @ids = tid
        @pl =  Teamplayer.joins(:live_player).where(:teamid => @ids).pluck(:Plyr, :Team)
  end
  if toimport == "true"
    @turl = Fteam.where(:id => @ids).pluck(:TeamUrl)

    @turl = @turl[0]  

    system "rake updateTm:updateA[#{@turl},#{@ids}]"
  end


end


# GET /teamplayers/1
# GET /teamplayers/1.json
def show
end

# GET /teamplayers/new
def new
  @teamplayer = Teamplayer.new
end

# GET /teamplayers/1/edit
def edit
end

# POST /teamplayers
# POST /teamplayers.json
def create
  @teamplayer = Teamplayer.new(teamplayer_params)

  respond_to do |format|
    if @teamplayer.save
      format.html { redirect_to @teamplayer, notice: 'Teamplayer was successfully created.' }
      format.json { render action: 'show', status: :created, location: @teamplayer }
    else
      format.html { render action: 'new' }
      format.json { render json: @teamplayer.errors, status: :unprocessable_entity }
    end
  end
end

# PATCH/PUT /teamplayers/1
# PATCH/PUT /teamplayers/1.json
def update
  respond_to do |format|
    if @teamplayer.update(teamplayer_params)
      format.html { redirect_to @teamplayer, notice: 'Teamplayer was successfully updated.' }
      format.json { head :no_content }
    else
      format.html { render action: 'edit' }
      format.json { render json: @teamplayer.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /teamplayers/1
# DELETE /teamplayers/1.json
def destroy
  @teamplayer.destroy
  respond_to do |format|
    format.html { redirect_to teamplayers_url }
    format.json { head :no_content }
  end
end

private
  # Use callbacks to share common setup or constraints between actions.
  def set_teamplayer
    @teamplayer = Teamplayer.find(params[:id])
  end

# Never trust parameters from the scary internet, only allow the white list through.
def teamplayer_params
  params.require(:teamplayer).permit(:playerid, :teamid)
end
end
那么这里出了什么问题,因为我注意到它调用了每个记录单独的页面,但为什么现在它将查询中的信息作为数据返回给我?

看起来像语句id=$teams.val;将返回undefined,因为没有id=teams的元素

如果未定义ID,则此函数调用中的数据可能为null或未定义:

function(data){
    $('#PlyrsTm2').html(data);
    alert("Loading Players...."); 
}
如果数据为null,调用htmlnull将导致所有表数据消失

因此,解决方案是正确地填充ID。我不确定ids应该做什么,但这很可能就是解决方案

$('#PlyrsTm2').html(data);
这就是为什么你的桌子不见了

问题是您正在替换此元素的内容,而不是元素本身。为了解决这个问题,我会这样做:

$('#SelectedTm').html(data);

您确定返回的数据不是空的吗?不,实际上我不确定如何检查console.log是您的朋友,请在成功函数中尝试console.logdata好的,它说未定义,但为什么现在返回任何内容?我想通过我这样做的方式,它会加载表中我需要的数据。它不能仅仅重建我的rails表。更改后,它仍然消失了,那么为什么它不用新数据重新绘制表呢?您从调用中收到了什么数据?以及查询中的活动记录。下面是数据中返回的内容:Object{playerid=1625,teamid=1,url=http://localhost:3000/teamplayers/527.json},对象{playerid=1625,teamid=1,url=http://localhost:3000/teamplayers/528.json},对象{playerid=1192,teamid=1,url=http://localhost:3000/teamplayers/529.json}我需要的是它给我控制器索引部分的数据。我已将我的控制器张贴在teams元素上方,该元素是一个下拉选择。当单击按钮ShowBTN时,设置ID。当我更新我的帖子时,请看我最近对我目前的情况所做的评论
$('#PlyrsTm2').html(data);
$('#SelectedTm').html(data);