AJAX atwho收听所有textarea id' s

时间:2017-11-05 16:46:48

标签: javascript jquery ruby-on-rails ajax coffeescript

我觉得这很容易我应该踢自己。我有这个coffeescript:

class @Invitation

@add_atwho = ->
$('#invitation_username_1543').atwho
  at: '@'
  displayTpl:"<li class='mention-item' data-value='(${image},${name})'><img src=${image}>@${name}</li>",
  callbacks: remoteFilter: (query, callback) ->
    if (query.length < 1)
      return false
    else
      $.getJSON '/mentionsy', { q: query }, (data) ->
        callback data

jQuery ->
  Invitation.add_atwho()

而不是听JUST #invitation_username_1543我想用我页面上的任何ID号替换1543。我也有这种局部模态渲染。 coffeescript正在听的问题是textarea id =&#34; invitation_username _ +#{x.id}&#34;。它适用于传入1543 id的模态,但没有别的!

<div class="modal fade" id="invite-<%= x.id %>" tabindex="-1" role="dialog" aria-labelledby="pirateinviteLabel" aria-hidden="true">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="inviteLabel">Invite a Friend!</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
        Text Text Text<br /><br />

        <%= form_for @invitations, :url => invitations_path, :method => 'create', remote: true do |b| %>

        <div class="form-group"><center>
          <b><%= b.label :username, 'Invite a friend by username:' %></b></center>
           <%= b.text_area :username, id: "invitation_username_#{+ x.id}", rows: 1, role: "textarea", class: 'form-control', required: true, placeholder: "@user1 @user2" %>
          <%= b.hidden_field :event_id, :value => x.id %>
        </div>
        <div class="text-center">
        <%= b.submit 'Send an Invite!', class: 'btn btn-primary btn-sm align-self-center' %>
      <% end %></div>

      </div>
    </div>
  </div>
</div>

1 个答案:

答案 0 :(得分:1)

您可以使用属性以选择器[attr^=value]开头,以选择attr属性以^=属性value

开头的元素
$("textarea[id^=invitation_username_]")

document.querySelectorAll("textarea[id^=invitation_username_]")