如何使用window.location.href

How to use window.location.href

本文关键字:href location window 何使用      更新时间:2023-09-26

我做"select",其中选项是'Edit', 'Delete'。当我单击"Edit"时,页面转到"edit_product.jsp"。但是当我单击"Delete"时,页面又转到"edit_product.jsp",而不是"delete_product.jsp"。我只想点击选择选项,这是另一个页面-"编辑"是"edit_product.jsp","删除"是"delete_product.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>

我使用jQuery和jsp。window.location.href可以只使用一个吗?我使用if else if,但对else if没有作用。我如何使用?

要获取所选的value name属性,请使用

if ($(this).find('option:selected').attr("name")==='edit') {
        window.location.href="edit_product.jsp?code="+$('.edit_select option[name=edit]:selected').val();
        return;
} else if($(this).find('option:selected').attr("name")==='delete')               {
        window.location.href="delete_product.jsp?code="+$('.edit_select option[name=delete]:selected').val();
        return;
}

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

是,您可以使用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" );