Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.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 Rails3 UJS更新未显示_Javascript_Jquery_Ruby On Rails_Ruby On Rails 3_Ujs - Fatal编程技术网

Javascript Rails3 UJS更新未显示

Javascript Rails3 UJS更新未显示,javascript,jquery,ruby-on-rails,ruby-on-rails-3,ujs,Javascript,Jquery,Ruby On Rails,Ruby On Rails 3,Ujs,我在获取更新页面显示的UJS调用时遇到问题。当选择框改变时,我想更新div的值。我在谷歌上搜索了所有地方,但都没有用 我使用Haml设置更改函数,如下所示: %script $("#timesheet_worker_id").change(function() { -# This works, so we know we're responding to the change event: -# alert('Fired the change function'); $.aja

我在获取更新页面显示的UJS调用时遇到问题。当选择框改变时,我想更新div的值。我在谷歌上搜索了所有地方,但都没有用

我使用Haml设置更改函数,如下所示:

%script
  $("#timesheet_worker_id").change(function() {
  -# This works, so we know we're responding to the change event: 
  -# alert('Fired the change function');
  $.ajax({url: "/timesheet_show_worker_name",
  async: false,
  data: 'selected=' + this.value,
  dataType: 'script'})
  });
  });
我的控制器具有以下功能:

def show_worker_name
  calculated = 'Adam Bilbo'
  render(:update) {|page|
    ### The following works just fine!
    #page << "alert('This is a test')"
    ### This doesn't; DOM is updated but not displayed; test data for now
    page << "$('#timesheet_worker_name').val('Adam Bilbo')"
  }
end
当ajax调用完成时,我检查DOM,值就在那里;i、 例如,在 控制台:

$('#timesheet_worker_id").val()
显示“Adam Bilbo”,但该值不显示在我的页面上

我正在运行Rails 3.0.9和GemjQueryRails 1.0.9;非常感谢您能帮我弄清楚我忽略了什么!谢谢


已解决-解决方案由@iwasrobed提供,如下所示。然而,最基本的问题是在更新“timesheet\u worker\u name”时使用val而不是文本,所以我最初的方法也适用于这种更改。

让我们稍微重新构造一下

:javascript
  $('#timesheet_worker_id').change(function() {
    $.ajax({
      url: '/timesheet_show_worker_name',
      data: { 'selected': $(this).value },
      success: function (json) { 
        $('#timesheet_worker_name').text(json.name);
      },
      error: function (response) {
        alert('Oops, we had an error.');
      }
    });
  });
您的控制器操作将是:

def show_worker_name
  calculated = 'Adam Bilbo'
  render :json => { :name => calculated }, :status => :ok
end

很抱歉和我原来的代码一样。页面上没有显示,但DOM值已设置。对不起,我甚至没有查看时间表\u worker\u name是什么。。。无法使用val设置段落p元素。您需要使用:$'timesheet\u worker\u name'。textjson.name;相反我更新了我的答案。再次抱歉!这就成功了;谢谢@Iwasrobed!这也意味着我的原始示例使用了相同的从val到text的更改。。。我测试过了。
def show_worker_name
  calculated = 'Adam Bilbo'
  render :json => { :name => calculated }, :status => :ok
end