如何使用window.location.href

时间:2015-07-07 04:51:06

标签: javascript java jquery jsp if-statement

我选择“选择”哪些选项是“编辑”,“删除”。当我点击“编辑”时,页面会很好地转到“edit_product.jsp”。但是当我点击“删除”时,页面再次转到“edit_product.jsp”,而不是“delete_product.jsp”。我想只点击选择选项,即其他页面 - “编辑”是“edit_product.jsp”,“删除”是“delete_product.jsp” - 。

这是代码。

[]

我使用jQuery和jsp。 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% request.setCharacterEncoding("utf-8"); %> <%@ page language="java" import="java.sql.*,java.util.*" %> <jsp:include page="../basis/basis.jsp" flush="false" /> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0" charset="UTF-8"> <link rel="StyleSheet" href="product.css" type="text/css"> <script src="../basis/basis.js"></script> <script type="text/javascript"> $(document).ready(function() { $(".all_check").click(function() { //select all .. }); $('.edit_select').change(function() { if ($('.edit_select option[name=edit]:selected')) { window.location.href="edit_product.jsp?code="+$('.edit_select option[name=edit]:selected').val(); return; } else if ($('.edit_select option[name=delete]:selected')) { window.location.href="delete_product.jsp?code="+$('.edit_select option[name=delete]:selected').val(); return; } }); }); </script> </head> <body> <div class="hhh"> All Products </div> <div class=" add_btn btn-group" role="group"> .. </div> <center> <div class="all_product"> <table class="table" data-toggle="table"> <tr> <th></th> <th> check <br /> <input type="checkbox" name="all_check" class="all_check" /> </th> <th>Product Code</th> <th>Image</th> <th>Price</th> <th>Status</th> <th>Business Group</th> <th>Category</th> <th>Stock</th> <th>Supplier</th> <th>Marketplaces</th> <!-- <th>Added Time</th> <th>Added User</th> <th>Modified Time</th> <th>Modified User</th> --> </tr> <% Vector product_code = new Vector(); Vector image = new Vector(); Vector price_cny = new Vector(); Vector status = new Vector(); Vector bus_grp = new Vector(); Vector category = new Vector(); Vector stock = new Vector(); Vector supplier = new Vector(); Vector market = new Vector(); Connection con= null; Statement st =null; ResultSet rs =null; String img = ""; String url = "http://localhost/Becomeus/Scripts/Products/image/"; try { Class.forName("org.gjt.mm.mysql.Driver"); } catch (ClassNotFoundException e ) { out.println(e); } try{ con = DriverManager.getConnection("jdbc:mysql://localhost/becomeus?useUnicode=true&characterEncoding=utf8","root","qlzjadjtm!2"); } catch (SQLException e) { out.println(e); } try { st = con.createStatement(); String sql = "select * from new_product"; rs = st.executeQuery(sql); if (!rs.next()) { out.println("No Product."); } else { do { product_code.addElement(rs.getString("product_code")); image.addElement(rs.getString("image")); price_cny.addElement(new Integer(rs.getInt("price_cny"))); status.addElement(rs.getString("status")); bus_grp.addElement(rs.getString("business_group")); category.addElement(rs.getString("category")); stock.addElement(new Integer(rs.getInt("stock"))); supplier.addElement(rs.getString("supplier")); market.addElement(rs.getString("marketplaces")); } while(rs.next()); int totalrows = product_code.size(); for (int j=0; j<=(totalrows-1); j++) { String pro_code = (String)product_code.elementAt(j); out.println("<tr>"); out.println("<td>"); //select out.println("<select name='edit_select' class='edit_select selectpicker' data-width='100px'>"); out.println("<option data-hidden='true'>-Select-"); out.println("<option name='edit' value="+product_code.elementAt(j)+">Edit"); out.println("<option name='delete' value="+product_code.elementAt(j)+">Delete"); out.println("<option value='duplicate'>Duplicate"); out.println("<option value='print'>Print"); out.println("<option value='viewrecord'>View Record"); out.println("</select>"); out.println("</td>"); out.println("<td>"); //check box out.println("<input type='checkbox' name='check' />"); out.println("</td>"); out.println("<td><a href='#' class='product_code'>"+product_code.elementAt(j)+"</a></td>"); out.println("<td><img width=100 height=100 src='image/"+image.elementAt(j)+"' /></td>"); out.println("<td>"+price_cny.elementAt(j)+"</td>"); out.println("<td>"+status.elementAt(j)+"</td>"); out.println("<td>"+bus_grp.elementAt(j)+"</td>"); out.println("<td>"+category.elementAt(j)+"</td>"); out.println("<td>"+stock.elementAt(j)+"</td>"); out.println("<td>"+supplier.elementAt(j)+"</td>"); out.println("<td>"+market.elementAt(j)+"</td>"); out.println("</tr>"); } rs.close(); } st.close(); con.close(); } catch (java.sql.SQLException e) { out.println(e); } %> </table> </div> </center> </body> </html> 只能使用一个吗?我使用的是window.location.href,但没有对if else if采取任何措施。我该怎么用?请回答我。

2 个答案:

答案 0 :(得分:0)

是的,您可以多次使用window.location.href

MDN window.location.href

window.location.href="edit_product.jsp?code="+$('.edit_select option[name=edit]:selected').val();

您也可以使用jQuery - load

$( "#result" ).load( "ajax/test.html" );

答案 1 :(得分:0)

要获取所选值名称属性,请使用此

cout << iter->segment << endl;

这应该会将您重定向到相应的页面。