weixin_33724046 2016-03-05 23:42 采纳率: 0%
浏览 18

表单不会与AJAX一起提交

app/assets/javascripts/profiles.js

This is my javascript for the form. The jQuery hide/show are working but the AJAX request is not..

$(document).on('page:load ready', function() {
  $('#pair-search').hide();
  $('#date-search').hide();
  $('input[type="radio"]').click(function() {
    if ($(this).attr("value") == "pair") {
        $("#pair-search").show();
        $("#date-search").hide();
    }
    else {
        $("#date-search").show();
          $("#pair-search").hide();
    }
  $('#search-form').on('submit', function(e) {
    e.preventDefault();
  $.ajax({
    url: $(this).attr('action'),
    type: $(this).attr('method'),
    dataType:'script',
    data: $(this).serialize(),
    success: function(data) {
      if (data) {
    $('#profiles').html(data);
  }
}
  });
});
  });
});

And here is the form on my profiles(#index) page ...it renders two partials. I will post the partial for "pair-search" so you can get an idea of how the forms work together.

app/assets/views/profiles/index.html.erb

User Search

<div id = "first_option">
  <%= form_tag users_path, method: :get, id: 'search-form' do %>
  <label>Who are you looking for?</label><br>
  <input type="radio" id=pairchoice name="choice" value="pair"> Paired <br>
  <input type="radio" id=datechoice name="choice" value="date"> Date <br>
  <% end %>
</div>

<div class = "choices">
  <div id = "pair-search">
    <%= render 'pair_search' %>
  </div>

  <div id = "date-search">
    <%= render 'date_search' %>
  </div>
</div>

<div id = "profiles">
</div>

Pair search partial:

app/assets/views/profiles/_pair_search

<%= form_tag(profiles_path, method: :get, :id =>'search-form') do %>
  <%= label(:language, 'What are your preferred languages?') %><br>
    <%= check_box_tag('search[language]', 'Ruby') %>
    <%= label_tag('search[language]', 'Ruby') %>
    <%= check_box_tag('search[language]', 'JavaScript') %>
    <%= label_tag('search[language]','JavaScript') %>
    <%= check_box_tag('search[language]', 'Python') %>
    <%= label_tag('search[language]', 'Python') %>
    <%= check_box_tag('search[language]', 'Java') %>
    <%= label_tag('search[language]','Java') %>
    <%= check_box_tag('search[language]', 'PHP') %>
    <%= label_tag('search[language]','PHP') %>
    <%= check_box_tag('search[language]', 'C++') %>
    <%= label_tag('search[language]','C++') %><br>

  <%=label_tag(:operating_system,"What is your Operating System of choice?") %><br>
    <%= check_box_tag('search[operating_system]', 'Unix') %>
    <%= label_tag('search[operating_system]','Unix') %>
    <%= check_box_tag('search[operating_system]', 'Linux') %>
    <%= label_tag('search[operating_system]','Linux') %><br>

  <%= label_tag(:age, 'What age-range are you looking for?')%><br>
    <%= check_box_tag('search[age]', '20-25') %>
    <%= label_tag('search[age]','20-25') %>
    <%= check_box_tag('search[age]', '25-30') %>
    <%= label_tag('search[age]', '25-30') %>
    <%= check_box_tag('search[age]', '30-35') %>
    <%= label_tag('search[age]', '30-35') %>
    <%= check_box_tag('search[age]', '40-45') %>
    <%= label_tag('search[age]','40-45') %><br>
    <%= check_box_tag('search[age]', '50+') %>
    <%= label_tag('search[age]','50+') %>

  <%= label_tag(:gender, "Gender:") %>
    <%= check_box_tag('search[male]') %>
    <%= label_tag('search[male]', 'Male' )%>
    <%= check_box_tag('search[female]')%>
    <%= label_tag('search[female]', 'Female')%>
    <%= check_box_tag('search[other]') %>
    <%= label_tag('search[other]', 'Other')%>


  <%= label_tag(:location, "Where do you want the person you're searching for to live?") %>
    <%= text_field_tag ('search[location]') %>

  <%= submit_tag("Search", :id => "search") %>
<% end %>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
    • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
    • ¥15 MATLAB中streamslice问题
    • ¥15 如何在炒股软件中,爬到我想看的日k线
    • ¥15 seatunnel 怎么配置Elasticsearch
    • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
    • ¥15 (标签-MATLAB|关键词-多址)
    • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
    • ¥500 52810做蓝牙接受端
    • ¥15 基于PLC的三轴机械手程序