function commit()
{
var length = window.document.location.href.indexOf(location.pathname);
var http = window.document.location.href.substring(0,length);
var v = $('#system_id').find(':selected').val();
window.location.href = ""+http+"/boards?system_id="+v+""; //这个是跳转到指定url
}
以http://localhost:3000/boards?system_id=000q000926XUkvMQawrCam 为例
location.pathname:/boards
location.href:http://localhost:3000/boards?system_id=000q000926XUkvMQawrCam 就是完整的url
location.href.indexOf(location.pathname):21 从开始到pathname之前有多少个字母
var http = window.document.location.href.substring(0,length); 从0到pathname之前的截取出来,因为当你在本地跑项目的时候,前缀是localhost,但实际开发项目中,可能时www开始的域名,所有跳转到指定的url并传参数,可以使用这种方法,既可以在本地跑,发布到服务器上也可以跑。
js调用 提交有一个onclick事件
js可以通过url进行值传递,具体写法是 ?加变量名=”+var变量+”
取得下拉框的值:$(‘#下拉框的name’).find(‘:selected’).val();
向页面发送请求,但是域名不是固定的不是localhost 需要把前面固定的截取出来
<div style="display:inline-block;">
<label for="order_system_type" style="display: inline-block">Select System:</label>
<%= select_tag_alias 'system_id', @system_ids %>
<input type="button" value="submit" onclick = "commit()">
</div>
select_tag_alias标签:第一个是下拉框的name,第二个是变量一个数组 数组第一个值是key就是显示的值 第二个值是value选中以后需要传的值 取出来的时候需要collect
system_id = params[:system_id];
@system_ids = []
@system_ids = Irm::ExternalSystemsTl.
where("#{Irm::ExternalSystemsTl.table_name}.external_system_id in (select external_system_id from #{Irm::ExternalSystemPerson.table_name} where person_id = '#{ Irm::Person.current.id }')").
where("#{Irm::ExternalSystemsTl.table_name}.language = 'en'").
collect { |i| [i[:system_name], i[:external_system_id]] }
没有collect的话 取出来的是一个Irm::ExternalSystemsTl对象,如果collect以后,是一个数组,数组key和value为第一个和第二个的值
<td class="label-col"><label><%= t(:knowledge_base) %></label></td>
<td class="data-col">
<!--<input type="checkbox" name="program_params[no_flag]" id="no_flag" onclick="set_value(this)"/>-->
<%= check_box_tag 'program_params[no_knowledge]' %>
显示一个选择框,当click时会触发一个set_value方法。
$(function(){
if('<%=program_params[:no_knowledge]%>' == '1'){
$("input[name='program_params[no_knowledge]']").prop("checked",true)
}
})
jquery:如果program_params[:no_knowledge]为1,则会被选中。