0

我第一次尝试了一些 ajax 但没有成功,我想使用 ajax 渲染部分单选按钮选择,这是我的代码:

我的脚本.js:

$(document).ready(function(){
$.ajax({
  url : "/income_vouchers/income_voucher_partial?$('form input[type=radio]:checked').val()="+$('form input[type=radio]:checked').val(),
  type: "GET",
  data: { 
        type: $('form input[type=radio]:checked').val() 
      }
});

 });

收入凭证.js.erb

$("#payment_mode").append('  

     <% if params[:type]== 'cheque' %> 
         <%= escape_javascript render :partial => "cheque" %>
     <% elsif params[:type]=='card' %> 
          <%= escape_javascript render :partial => "card" %>
     <% elsif params[:type]=='ibank'%> 
         <%= escape_javascript render :partial => "ibank" %>
      <% else params[:type] == 'cash' %>
    <% end %>
');

我来自:

<script type="text/javascript" src="/javascripts/voucher.js"></script>
<%= form_for(@income_voucher) do |f| %>
  <%= render 'shared/form_error', :object => @income_voucher %>
 <div >   
            <%= radio_button_tag :transaction_type,'cash', :checked => true %>
            <%= f.label :transaction_type, "Cash", :value => "cash" %>
            <%= radio_button_tag :transaction_type,'cheque' %>
            <%= f.label :transaction_type, "Cheque", :value => "cheque" %>
            <%= radio_button_tag :transaction_type,'card'%>
            <%= f.label :transaction_type, "Card", :value => "card" %>
            <%= radio_button_tag :transaction_type,'ibank'%>
            <%= f.label :transaction_type, "Internet Banking", :value => "ibank" %>
          </div>
      <div id = "payment_mode">
          <% if params[:type]== "cheque" %> 
                <%= render :partial => "cheque", :f => f %> 
          <% elsif params[:type]=='card' %> 
                <%= render :partial => "card", :f => f %>
          <% elseif params[:type] == 'ibank'%> 
              <%= render :partial => "ibank", :f => f %>
          <% else params[:type] == 'cash' %> 
       <% end %>
     </div>
  <%= f.submit %>
<% end %>

这是我的控制器方法:

 class IncomeVouchersController < ApplicationController
def new
    @income_voucher = IncomeVoucher.new
    @invoices = current_company.invoices
    @income_voucher.build_payment_mode

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @income_voucher }
    end
  end
 def create
    @income_voucher = IncomeVoucher.new(params[:income_voucher])

   transaction_type = params[:transaction_type]
    payment_mode = nil
    if transaction_type == 'cheque'
      payment = ChequePayment.new(params[:cheque_payment])
    payment.amount = @income_voucher.amount
    elsif transaction_type == 'card'
      payment = CardPayment.new(params[:card_payment])
    payment.amount = @income_voucher.amount
    elsif transaction_type == 'ibank'
      payment = InternetBankingPayment.new(params[:internet_banking_payment])
    payment.amount = @income_voucher.amount
    else
      payment = CashPayment.new
    payment.amount = @income_voucher.amount
    end
    payment_mode = PaymentMode.new
    payment_mode.transactionable = payment

    @income_voucher.payment_mode = payment_mode
    respond_to do |format|
      if @income_voucher.save

        format.html { redirect_to(@income_voucher, :notice => 'Income voucher was successfully created.') }
        format.xml  { render :xml => @income_voucher, :status => :created, :location => @income_voucher }
      else

        format.html { render :action => "new" }
        format.xml  { render :xml => @income_voucher.errors, :status => :unprocessable_entity }
      end
    end

   def income_voucher_partial
       @payment_mode = PaymentMode.new(params[:payment_mode])
  end 
 end

i can see my view form but when select a radio button nothing is rende. 任何帮助,将不胜感激

4

2 回答 2

1

首先:选择方法——或者你使用远程表单,或者ajax提交。如果您希望该用户选择单选按钮并单击提交以在 .html.erb 中呈现使用:

form_for(@income_voucher, :remote => true)

删除 javascript ajax 并将 .js.erb 中的参数名称更改为 :transaction_type

如果您希望该用户选择单选按钮并立即渲染将 javascript ajax 移动到单选按钮更改事件处理程序,如下所示

$(your_element).change(function(){
  $.ajax(){
    url : "/income_vouchers/income_voucher_partial",
    data: { 
      type: $('form input[type=radio]:checked').val() 
    }
  }
});

并直接从 url 中删除参数设置 - 在“数据:”中设置参数

于 2012-05-30T09:44:29.183 回答
0

在我的 js.erb 文件中,我这样做了

$(".gradeU").remove();
    $("#abcd").after(  

    '<%= escape_javascript render :partial => params[:payment] unless params[:payment] == 'cash' %>'
    );

在我的脚本中:

$(document).ready(function(){
$("#check").click(function(){

 var payment = $('form input[type= radio]:checked').val()

 console.debug(payment); 

 $.ajax({

   type: 'GET',

   url : "/income_vouchers/income_voucher_partial?payment="+payment,

 });

 });
 });

而且我没有在表单中呈现相同的代码,谢谢

于 2012-05-30T10:56:27.193 回答