来自javascript的动态按钮值?

时间:2018-07-16 20:03:59

标签: javascript ruby-on-rails ajax

我有一个使用javascript进行搜索的页面,我想获取它附带的用户列表,并将其作为提交发送到下一页。我所拥有的是:

  .search_client_users
    = form_tag admin_clients_path, method: "get" , class: "search_form" do
      = label_tag 'search_term', 'Old domain name:'
      = text_field_tag 'search_term', nil, autocomplete: "off", size: "50"      
    .main_form.client_emails
      = simple_form_for(:domainNameSwap, url: { action: "update" }, html: { method: :put }) do |f|
        .input-row
          = f.hidden_field :users, value: @clients    
        .submit-row
          .row
      .col-xs-5
         .submit
           = f.submit "Update domains", id: "submit", :class => "btn btn-primary submit"
    .client_list

- content_for :javascript do
  = javascript_include_tag 'admin/search_client_users'

[由于剪切和粘贴,某些格式可能不正确,对不起]

admin / search_client_users创建一个@clients,我至少可以确定:

class App.ClientUserList
  constructor: ->
    @incrementalSearchAttempts = 0

  search: (searchTerm, completeCallback) =>
    handleResponseWithOrderAwareness = (attemptNumber, response) =>
      if attemptNumber >= @incrementalSearchAttempts
        completeCallback(response)

    @incrementalSearchAttempts++
    onComplete = _.partial(handleResponseWithOrderAwareness, @incrementalSearchAttempts)
    $.get('/admin/manage_clients/client_list', { search_term: searchTerm }).complete(onComplete)

class App.Views.SearchClientUsers extends Backbone.View
  events:
    "keyup input[name='search_term']": "search",
    "click .profile_attribute": "showClientUserProfile"

  initialize: =>
    @clientUserList = new App.ClientUserList()

  search: =>
    searchTerm = $('.search_form input[name=search_term]').val()
    @clientUserList.search(searchTerm, @render)

  showClientUserProfile: (event) =>
    window.location = $(event.currentTarget).closest('tr').data('client-path')

  render: (response) =>
    @$el.find('.client_list').html(response.responseText)

$ ->
  new App.Views.SearchClientUsers(el: $('.search_client_users')).search()

所以,我正在尝试获取客户端列表,并将其发送到控制器中的update方法。但是,由于发生javascript和ruby时,它似乎不起作用...有没有办法做到这一点?还是我必须弄清楚如何在Ajax中做到这一点?

ETA:另一种想法是,我想只将初始text_field转换为表单,以便将text字段用于javascript,然后将THEN提交给表单,然后进行更新即可搜索...我的数据集足够小,以至于两次搜索都不是我认为的大问题...

但是我不太确定如何将两种形式合并在一起...

0 个答案:

没有答案