【问题标题】:Rails 4 get value from text_field_tag that contains javascript varRails 4 从包含 javascript var 的 text_field_tag 获取值
【发布时间】:2016-08-23 13:01:54
【问题描述】:

我正在从 rails 2.3.10 迁移到 rails 4,并且我有一个用于在手机上向患者时间表发送消息的应用程序。当我选择一个或多个并发送消息时,我的应用程序会显示日程安排的患者列表,并且每一行都存在一个 check_box_tag。 所以我有一个调用部分的表单,这个部分包含一个带有 check_box_tag 的列表。选中的每个复选框都存储在 javascript 变量 selected_ids 上,并在主表单中的 text_field_tag 上返回。问题出在事件 link_to 参数 selected_ids 为空,但我可以在 check_box_tag 上看到 下面的部分形式,部分和javascript:

function MultiSelectIDs(FieldName) {
  var selected_ids = new Array()
  var objCheckBoxes = document.getElementsByName(FieldName);
  for(var i = 0; i < objCheckBoxes.length; i++){
    if (objCheckBoxes[i].checked) {
      var x = selected_ids.splice(selected_ids.length,0, objCheckBoxes[i].id);
    }
  }
  document.getElementById('selected_ids').value = selected_ids.join(", ")
};
<% if !@tagendamento.blank? && !@tagendamento.nil? %>
  <% for tagendamento in @tagendamento do %>
  <tr>
  <%= check_box_tag "#{tagendamento.id}", 0, false, :name=> "chk_agendamento", :onclick => "MultiSelectIDs('chk_agendamento')" %>
<% end %>
  
<%= text_field_tag :selected_ids %>
<%= link_to 'Pré-Visualizar SMS',send_sms_queries_path(:sel_ids=>params[:selected_ids],:perfil=>params[:perfil], :visualizar => '1', :dt_envio=>(params[:dt_envio].nil?)? (Date.today - 3) : params[:dt_envio],:t_envio=>params[:t_envio],:tipo_msg=>params[:tipo_msg],:mensagem=>(!params[:mensagem].nil? and !params[:mensagem].blank?)? params[:mensagem] : @standard,:selec_tipotemplate_sms=>params[:selec_tipotemplate_sms],:date_agend_hc=>params[:date_agend_hc]), :target=>'_blank', :style =>	"color:black"%>
    <%= text_field_tag :selected_ids %>
    <%= link_to 'Pré-Visualizar SMS', send_sms_queries_path(:sel_ids        =>  params[:selected_ids],
                                                            :perfil         =>  params[:perfil],
                                                            :visualizar     =>  '1',
                                                            :dt_envio       => (params[:dt_envio].nil?)? (Date.today - 3) : params[:dt_envio],
                                                            :t_envio        =>  params[:t_envio],
                                                            :tipo_msg       =>  params[:tipo_msg], 
                                                            :mensagem       => (!params[:mensagem].nil? and !params[:mensagem].blank?)? params[:mensagem] : @standard,
                                                            :selec_tipotemplate_sms =>  params[:selec_tipotemplate_sms], 
                                                            :date_agend_hc  =>  params[:date_agend_hc]), 
                                    :target =>  '_blank', 
                                    :style  =>  "color:black"%>

【问题讨论】:

    标签: javascript checkbox textfield link-to


    【解决方案1】:

    终于成功了! 在onclick link_to中添加text_field_tag内容如下:

    <%= link_to 'Pré-Visualizar SMS', send_sms_queries_path(:selected_ids	=>params[:selected_ids]),  
    :target	=>	'_blank', 
    :style	=>	"color:black",
    :onclick => "this.href = this.href + '&selected_ids=' + selected_ids.value"%>

    谢谢大家!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-22
      • 1970-01-01
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-30
      相关资源
      最近更新 更多