我在下拉时遇到问题。当我在下拉列表中选择默认值--select--时,它会呈现整个表单。
<%= render 'leave_applies/sidebar' %>
<% content_for :script do %>
<%= javascript_include_tag :leave_applies %>
<% end %>
<div class='row-fluid clear'>
<div class='box gradient'>
<div class='title'>
<h3 style='margin-left:1em'>Leave Summary</h3>
</div>
<div class='content'>
<div class="control-group string optional student_gender">
<label class="string optional control-label" for="student_gender">Teacher Code</label>
<div class="controls">
<%= select_tag "leaves", options_from_collection_for_select(Teacher.all, "id", "teacher_code"), :prompt => "--Select Employee No--" %>
</div>
</div>
<div id = "leaves_apply">
</div>
</div>
</div>
</div>
在此代码中,当我选择“ - 选择员工编号 - ”时,它会呈现整页
Js代码
$("#leaves").change(function(){
var url = '/leave_summary?summary=' + $(this).val()+''
$.ajax({
url: url,
dataType: 'html',
type: 'GET',
success: function(data){
$("#leaves_apply").html(data);
}
});
})
答案 0 :(得分:0)
你必须在提交ajax请求之前检查,否则值是空的或者不是基于那个fire ajax查询
$("#leaves").change(function(){
if($(this).val() != "" )
{
var url = '/leave_summary?summary=' + $(this).val()+''
$.ajax({
url: url,
dataType: 'html',
type: 'GET',
success: function(data){
$("#leaves_apply").html(data);
}
});
}
})