添加提交按钮调用先前按钮的功能本身 [英] Added submit button calls the earlier button's function itself

查看:44
本文介绍了添加提交按钮调用先前按钮的功能本身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发 Rails 3 应用.这里有一个视图显示用户上传的所有 excel 文件.一个用于执行所有复选框选定文件的按钮已经存在,并分别删除每个文件.现在我应该添加一个删除"按钮来删除选定的文件.我添加了按钮 n 修改了此表单调用的函数现在只显示执行中"和删除中" 以检查第二个删除"按钮是否具有功能.但是每次单击删除时,它只会在 cmd 中打印in execute".我猜在视图列表"中编写的 AJAX 相关代码是问题所在.请帮忙!!告诉我为什么它会一直执行?相关代码在这里:

PS:我使用过 if params[:commit]="Delete" &if params[:delete_button] 也在 controller.rb 中但没有帮助

list.html.erb(显示所有文件的视图)

<% if @files.length >0%><h2 id='comments'>上传的 Excel 文件如下所列,用于编辑/删除/执行</h2><div id='checkone' class='hide'>请检查至少一个要执行的excel文件</div><% ajax_str = "new Ajax.Request('/account/execute_testcases', {asynchronous:true, evalScripts:true, onComplete:function(request){adjust_sidebar();Element.show('msg');Element.hide('waitid');Element.hide('disableexecuteid');Element.show('executeid');}, onLoading:function(request){Element.show('waitid');Element.hide('msg');Element.hide('executeid');Element.show('disableexecuteid');}, parameters:Form.serialize(this)}); return false;".html_safe %><%= form_for 'file_names', :url =>{:控制器=>'帐户', :action =>'execute_testcases'}, :remote =>真,:html =>{:name =>'frmExecute', :onsubmit =>ajax_str }, :id =>'execute_tc' 做 |f|%><表格><% if @file_count >1 &&@error_in_all_files == false %><tr><td><input type='checkbox' name='chkAll' onclick='checkAll();'><span class='text'>全部勾选/全部取消</span></td></tr><%结束%><表类='上传'><% for a in @files %><tr><td><% file_id = a.id.to_i %><% if(@excel_errors[file_id].nil? || @excel_errors[file_id].empty?) &&a.file_type.to_i != 1 %><input type='checkbox' name = "excelfile[]" value="<%= a.excel_filename %>,<%= a.excel_filename_with_timestamp %>"><%其他%><input type='checkbox' name = "excelfile[]" value="<%= a.excel_filename %>,<%= a.excel_filename_with_timestamp %>"禁用=真><%结束%><a href="open_excel_file/<%= a.id %>"title='点击打开' class='nodecoration'><%= a.excel_filename %></td><td><%= link_to(image_tag("/images/b_edit.png", :border => 0, :title => 'Edit'), :action => 'upload_file', :file_id => a.id)%></td><td><a href="delete/<%=a.id %>"><img src='/images/b_drop.png' border=0 title='Delete' onclick="return confirm('This也会删除相关报告.您确定要删除吗?');"></a></td><td><%如果 !@excel_errors[file_id].nil?&&!@excel_errors[file_id].empty?@joined_excel_errors = @excel_errors[file_id].join(', ')%><a href='#' onclick="show_excel_errors(<%=file_id%>);"title="错误">错误<%结束%></td></tr><tr id="excel_error_<%=file_id %>"style='display:none;'><td colspan=4><% if !@excel_errors[file_id].nil?&&!@excel_errors[file_id].empty?%><div class="padder"><% for error_value in @excel_errors[file_id] %><font color='maroon'><%= error_value %></font><br><%结束%>

<%结束%></td></tr><%结束%><tr><td>&nbsp;</td></tr><tr><td><% if @error_in_all_files == false %><span class='executebutton' id='executeid'><%= f.submit "Execute", name: 'execute_button', :onclick =>"return checkSelected();"%></span><span class='deletebutton' id='deleteid'><%= f.submit "Delete", name: 'delete_button', :onclick =>"return checkSelected();"%></span><%结束%><span id='disableexecuteid' class='executebutton' style='display:none;'><input type='submit' value="Execute" disabled="disabled"></span><span id='waitid' style="display:none;"类='文本'><br>正在执行测试用例...请稍候...<%= image_tag("/images/wait26trans.gif", :border => 0) %></span><span id='msg' style="display:none;"类='文本'><br><br>单击此处<%= link_to '查看测试结果',{:controller =>'帐户', :action =>'recent_test_results'}, :class =>'棕色链接' %></span></td></tr><span id='subject_list'></span><%结束%><%其他%>未找到测试用例表!<br><br><%=link_to'>>上传文件', {:controller =>'帐户', :action =>'upload_file'}, :class =>'棕色链接' %><%结束%><% for i in 1..10%><div>&nbsp;</div><%结束%>

controller.rb

 def execute_testcases如果 !params[:execute_button].nil?把执行"# 文件名 = []# originalfile_filewithtime = []# original_file_map = {}# originalfile_filewithtime = params[:excelfile]......# 这里有一些代码......# render :update do |page|# page.replace_html :subject_list, :partial =>'show_output', :locals =>{:new_file_map =>@new_file_map}# page.visual_effect :highlight, 'subject_list', :duration =>2# flash[:display]=@execmsg#结尾# 放置@execmsgelsif !params[:delete_button].nil?把删除"结尾结尾

解决方案

我意识到它必须对 AJAX 的 Form.serialize(this) 做一些事情,它可以序列化所有内容.即无论是否存在提交按钮,它都会调用视图中第一个按钮的功能

I'm working on Rails 3 app. Here a view displays all the excel files uploaded by user. A button to execute all the checkbox selected files was already present with delete for each file separately. Now I'm supposed to add a 'delete' button to delete the selected files. I've added the button n modified the function called by this form jus to display "in execute" and "in delete" for now to check if the second 'delete' button is functioanal. But every time delete is clicked it prints "in execute" only in cmd. I guess the AJAX related code written in the view 'list' is the problem. Pls help!! Tell me why is it going to execute always? The related code is here:

PS: I hav used if params[:commit]="Delete" & if params[:delete_button] also in controller.rb to but didnt help

list.html.erb (the view that displays all files)

<% if @files.length > 0 %>
<h2 id='comments'>Uploaded Excel Files are as listed below for Edit/Delete/Execution</h2>
<div id='checkone' class='hide'>Please check atleast one excel file to execute</div>
    <% ajax_str = "new Ajax.Request('/account/execute_testcases', {asynchronous:true, evalScripts:true, onComplete:function(request){adjust_sidebar();Element.show('msg');Element.hide('waitid');Element.hide('disableexecuteid');Element.show('executeid');}, onLoading:function(request){Element.show('waitid');Element.hide('msg');Element.hide('executeid');Element.show('disableexecuteid');}, parameters:Form.serialize(this)}); return false;".html_safe %>
    <%= form_for 'file_names', :url => {:controller => 'account', :action => 'execute_testcases'},  :remote => true, :html => {:name => 'frmExecute', :onsubmit => ajax_str }, :id =>'execute_tc' do |f| %>
    <table>
        <% if @file_count > 1 && @error_in_all_files == false %>
        <tr>
        <td>
        <input type='checkbox' name='chkAll' onclick='checkAll();'>
        <span class='text'>Check All/Decheck All</span>
        </td>
        </tr>
        <% end %>
    </table>
    <table class='upload'>
    <% for a in @files %>
        <tr>

        <td>
        <% file_id = a.id.to_i %>
        <% if(@excel_errors[file_id].nil? || @excel_errors[file_id].empty?) && a.file_type.to_i != 1 %>
            <input type='checkbox' name = "excelfile[]" value="<%= a.excel_filename %>,<%= a.excel_filename_with_timestamp %>">
        <% else %>
            <input type='checkbox' name = "excelfile[]" value="<%= a.excel_filename %>,<%= a.excel_filename_with_timestamp %>" disabled=true>
        <% end %>
        <a href="open_excel_file/<%= a.id %>" title='Click to open' class='nodecoration'><%= a.excel_filename %></a>
        </td>
        <td>
        <%= link_to(image_tag("/images/b_edit.png", :border => 0, :title => 'Edit'), :action => 'upload_file', :file_id => a.id) %>
        </td>
        <td>
        <a href="delete/<%=a.id %>"><img src='/images/b_drop.png' border=0 title='Delete' onclick="return confirm('This will delete the related reports too. Are you sure to delete?');"></a>


        </td>
        <td>
            <% 
               if !@excel_errors[file_id].nil? && !@excel_errors[file_id].empty? 
                 @joined_excel_errors = @excel_errors[file_id].join(', ')
            %>     
                <a href='#' onclick="show_excel_errors(<%=file_id%>);" title="Error">Error</a>
            <% end %>
        </td>

        </tr>
        <tr id="excel_error_<%=file_id %>" style='display:none;'>
           <td colspan=4>
            <% if !@excel_errors[file_id].nil? && !@excel_errors[file_id].empty? %>
                <div class="padder">
                <% for error_value in @excel_errors[file_id] %>
                    <font color='maroon'><%= error_value %></font><br>
                <% end %>
                </div>
            <% end %>
            </td>
        </tr>
    <% end %>
        <tr><td>&nbsp;</td></tr>

        <tr>
        <td>
        <% if @error_in_all_files == false %>
        <span class='executebutton' id='executeid'>
            <%= f.submit "Execute", name: 'execute_button', :onclick =>"return checkSelected();" %>
        </span>
        <span class='deletebutton' id='deleteid'>
            <%= f.submit "Delete", name: 'delete_button', :onclick =>"return checkSelected();" %>
        </span>
        <% end %>
        <span id='disableexecuteid' class='executebutton' style='display:none;'>
        <input type='submit' value="Execute" disabled="disabled">
        </span>
        <span id='waitid' style="display:none;" class='text'>
            <br>Executing Test Cases...Please wait...<%= image_tag("/images/wait26trans.gif", :border => 0) %>
        </span>
        <span id='msg' style="display:none;" class='text'>
            <br><br>  Click here to <%= link_to 'View Test Results', {:controller => 'account', :action => 'recent_test_results'}, :class => 'brownlink' %>
        </span> 
        </td>
        </tr>
    </table>
    <span id='subject_list'>    
    </span>
    <% end %>
<% else %>
No test case sheets found! 
    <br><br>
    <%= link_to '>> Upload File', {:controller => 'account', :action => 'upload_file'}, :class => 'brownlink' %>
<% end %>
<% for i in 1..10 %>
    <div>&nbsp;</div>
<% end %>

controller.rb

  def execute_testcases
  if !params[:execute_button].nil? 
    puts "in execute"
    # file_names = []
    # originalfile_filewithtime = []
    # original_file_map = {}
    # originalfile_filewithtime = params[:excelfile]
    ......
    # SOME CODE HERE
    ......
    # render :update do |page|
      # page.replace_html :subject_list, :partial => 'show_output', :locals => {:new_file_map => @new_file_map}
      # page.visual_effect :highlight,  'subject_list', :duration => 2
      # flash[:display]=@execmsg
    #end
    # puts @execmsg

  elsif !params[:delete_button].nil? 
   puts "in delete"
  end

  end  

解决方案

I realised it had to do something with the Form.serialize(this) of AJAX which serialises everything. ie It calls the function of first button in the view irrespective of no of submit buttons present

这篇关于添加提交按钮调用先前按钮的功能本身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆