Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.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
Jquery 使用ajax创建第二个下拉列表_Jquery_Ruby On Rails_Ajax_Ruby On Rails 3.2 - Fatal编程技术网

Jquery 使用ajax创建第二个下拉列表

Jquery 使用ajax创建第二个下拉列表,jquery,ruby-on-rails,ajax,ruby-on-rails-3.2,Jquery,Ruby On Rails,Ajax,Ruby On Rails 3.2,我目前的状态是: 视图: 参赞咖啡 jQuery -> $('.counselor_supervisor_employee_id').hide() employee = $('#counselor_supervisor_employee_id').html() $('#counselor_supervisor_employee_position_id').change -> position = $('#counselor_supervisor_employee_

我目前的状态是:

视图:

参赞咖啡

jQuery ->
  $('.counselor_supervisor_employee_id').hide()
  employee = $('#counselor_supervisor_employee_id').html()
  $('#counselor_supervisor_employee_position_id').change ->
    position = $('#counselor_supervisor_employee_position_id :selected').text()
    escaped_position = position.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
    options = $(employee).filter("optgroup[label='#{escaped_position}']").html()
    if options
      $('#counselor_supervisor_employee_id').html(options)
      $('.counselor_supervisor_employee_id').fadeIn()
    else
      $('#counselor_supervisor_employee_id').empty()
      $('.counselor_supervisor_employee_id').hide()

coffee脚本代码来自此屏幕广播,但使第二个下拉列表仅显示/隐藏对我来说不是一个好的解决方案,我需要发送一个ajax请求以获取第二个下拉列表的数据,我还将添加一个beforeSend is loading,在这种情况下,如何添加ajax请求?我已经寻找了很多,但我不能达到我想要的,我也试图遵循,但它没有为我工作。我的rails版本是3.2.14

我建议使用我已经在一些项目中使用的以下方法

在第一个下拉列表中附加一个on change事件侦听器 当事件被触发时,使用AJAX调用检索正确的JSON数据 在AJAX调用的回调中,使用检索到的数据重新填充第二个下拉列表。 共同脚本:

    $('#counselor_supervisor_employee_position_id').change ->
      $.get('YOUR ROUTE HERE' + position_id + '/RESOURCES.json', (result) ->
      options = []
      $.each result, ->
        options.push($("<option />").val(this.RESOURCE_id).text(this.RESOURCE_NAME))
        event.field.find(SECOND_DROPDOWN_HERE).find('option').remove().end().append(options)
      )
现在,您只需要在操作中响应JSON

如果答案不清楚,或者我忘记了一些细节,请告诉我

jQuery ->
  $('.counselor_supervisor_employee_id').hide()
  employee = $('#counselor_supervisor_employee_id').html()
  $('#counselor_supervisor_employee_position_id').change ->
    position = $('#counselor_supervisor_employee_position_id :selected').text()
    escaped_position = position.replace(/([ #;&,.+*~\':"!^$[\]()=>|\/@])/g, '\\$1')
    options = $(employee).filter("optgroup[label='#{escaped_position}']").html()
    if options
      $('#counselor_supervisor_employee_id').html(options)
      $('.counselor_supervisor_employee_id').fadeIn()
    else
      $('#counselor_supervisor_employee_id').empty()
      $('.counselor_supervisor_employee_id').hide()
    $('#counselor_supervisor_employee_position_id').change ->
      $.get('YOUR ROUTE HERE' + position_id + '/RESOURCES.json', (result) ->
      options = []
      $.each result, ->
        options.push($("<option />").val(this.RESOURCE_id).text(this.RESOURCE_NAME))
        event.field.find(SECOND_DROPDOWN_HERE).find('option').remove().end().append(options)
      )