Ruby中的变量在rails函数中的JavaScript [英] ruby variable in javascript in rails function

查看:101
本文介绍了Ruby中的变量在rails函数中的JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图根据rails数据库值更改一行的颜色。在行内也是一个下拉菜单。在更改下拉列表中的选定值时,我通过表单提交将更改后的值更新到数据库,然后调用javascript函数通过AJAX使用新值更改行颜色。



html.erb:

 <%= f.select(:status,[to call, 未连接,确认,拒绝],{:selected => lead.status},:onchange =>%Q [$('#lead_form _#lead.id}')。submit (); document.getElementById('lead_row _#{lead.id}')。bgcolor = Application.getRowColour(#{lead.status});])%> 

在上面的代码中,发生的事情是传递给#{lead.status}当我第一次加载页面时,getRowColour总是相同的,即状态的初始值。所以有多少次我通过下拉改变状态,getRowColour()不会改变。



网页来源:

  $( '#lead_form_133')提交();的document.getElementById( 'lead_row_133')BGCOLOR = Application.getRowColour( 确认)。 

可以看出,getRowColour()正在取一个常数值,呼叫。

解决方案

假设状态的标识选择框是 status 请考虑使用以下结构:

 <%= f.select(:status,[to call,did not connect,confirmed,rejected],
{:selected => lead.status然后添加 onchange

处理器逻辑:

 < script type =text / javascript> 
$(#status) (){
$('#lead_form _#{lead.id}')。submit();
$('#lead_row _#{lead.id}')
.css('background-color',Application.getRowColour($(#status).val())
});
< / script>


I am trying to change the colour of a row according to a rails database value. Also within the row is a form which is a drop down menu. On changing the selected value in the drop down form I update the changed value to the DB by a form submit and then call a javascript function to change the row colour using the new value through AJAX.

html.erb:

<%= f.select(:status, ["to call","didn't connect","confirmed","rejected"], {:selected => lead.status}, :onchange => %Q[$('#lead_form_#{lead.id}').submit();document.getElementById('lead_row_#{lead.id}').bgcolor=Application.getRowColour("#{lead.status}");]) %>

In the above code what is happening is that the #{lead.status} which is being passed to getRowColour is always the same i.e. the initial value of status when I first load the page. So how many ever times I change the status via the dropdown, the getRowColour("") does not change.

source of the page:

$('#lead_form_133').submit();document.getElementById('lead_row_133').bgcolor=Application.getRowColour("confirmed");

As can be seen the getRowColour() is taking a constant value and not re-evaluating it on every call. How can I send my lastest status param to this function?

解决方案

Assuming the id of the status select-box is status consider using the following construction:

<%= f.select(:status, ["to call","didn't connect","confirmed","rejected"], 
             {:selected => lead.status} %>      

Then add the onchange handler logic:

<script type="text/javascript">
    $("#status").change(function() {
        $('#lead_form_#{lead.id}').submit();
        $('#lead_row_#{lead.id}')
          .css('background-color', Application.getRowColour($("#status").val())
    }); 
</script>

这篇关于Ruby中的变量在rails函数中的JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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