0
我是一名初学者,在红宝石轨道上进行开发。 我想要做的是:从数据库搜索记录并将它们添加到列表中
- 创建一个输入搜索来搜索产品。
- 然后,我点击一个提交按钮,这个产品将显示在下面的列表中。
- 我搜索另一个产品,它将被添加到我的清单。
- 最后,我会列出搜索到的产品。
<script type="text/javascript">
var foodList = [];
function addToFood() {
var addFood = document.getElementById('addFood').value;
var addQuantity = document.getElementById('addFood').value;
foodList.push(addFood + " " + addQuantity);
for (i = 0; i < foodList.length; i++) \t {
var newFood = "<a href='#' onClick='removeRecord(" + i + ");'>X</a> " + foodList[i] + " <br>";
};
document.getElementById('foods').innerHTML += newFood;
document.getElementById('addQuantity').value = '';
document.getElementById('addFood').value = '';
}
function removeRecord (i) {
foodList.splice(i, 1);
var newFood = "";
// re-display the records from foodList the same way we did it in addToFood()
for (var i = 0; i < foodList.length; i++) {
newFood += "<a href='#' onClick='removeRecord(" + i + ");'>X</a> " + foodList[i] + " <br>";
};
document.getElementById('foods').innerHTML = newFood;
}
</script>
<%= form_tag akala_health_path, :method => 'get', :id => "products_search" do %>
<p>
Product name
<%= text_field_tag :search, params[:search] , :id => "addFood"%></br>
<%= submit_tag "Search", :name => nil %>
</p>
<% end %>
<table id="products">
<tr>
<th>Product name</th>
<th>Product code</th>
<th>Product quantity</th>
<th></th>
</tr>
<% if @products.count == 0 %>
<% else %>
<% @products.each do |product| %>
<tr>
<td id="addFood"><%= product.product_name %></td>
<td id="addFood"><%= product.product_code %></td>
<td><input type="text" value="" /></br></td>
<td><input type="submit" value="Ajouter" onClick="addToFood();"> </td>
</tr>
<% end %>
<% end %>
</table>
<%= will_paginate @products%>
<!-- The list of food is displayed in the following div -->
<div id="foods"></div>
我怎样才能做到这一点?请帮帮我。