5

我有main.jsp,其中包括 4 个 jsp 页面,即company.jsp、software.jsp、location.jsp、dept.jsp
,最后将是提交按钮,该按钮将被重定向到另一个 jsp 页面

注意:在 company.jsp 和 Software.jsp 中,只需要单击一个单选按钮
在 location.jsp 中,dept.jsp 具有带有多个复选框的复选框

我想细化我的搜索 类似这样的:

在此处输入图像描述

我希望当基于公司选择的软件在 company.jsp 中选择一个单选按钮时,将从数据库中显示。And when Software is selected locations with checkboxes will appear based on database.Then if locations are selected depts will be displayed based on database and so on..

我尝试使用 jquery,其中选定的单选按钮作为参数添加到 url 但这是不好的做法,因为我正在通过window.location.href. 那是不好的做法
使用 servlet 或 jquery 的任何其他想法?

编辑:

主.jsp

<div style="background-color:#F4F4F4"><b>Company:</b></div>
<div id="company"> <jsp:include page="company.jsp"></jsp:include> </div>
<div style="background-color:#F4F4F4"><b>Software:</b></div>
<div id="software">
<jsp:include page="software.jsp"></jsp:include> </div>
<div style="background-color:#F4F4F4"><b>Location:</b></div>
<div id="location">
<jsp:include page="my_location.jsp"></jsp:include> </div>


公司.jsp

<script src="jquery-1.9.1.min.js"></script>
<script src="jquery-1.9.1.js"></script>
<script type="text/javascript">
function call(a)
{
    window.location.href="main.jsp?company="+a ;
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
Connection conn = null;
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","","");
ResultSet rs = null;
Statement st=null;
st=con.createStatement();
%>

<form name=company_form id=company_form>
<%
String name=request.getParameter("r");
if(name!=null){
//out.println("value in company.jsp:"+name);
}

%>
<br>
<%
try{
rs = st.executeQuery("select DISTINCT(COMPANY) from maindatabase");
int i=0; 
while(rs.next())
  {
%>
<input type="radio" name="r" onclick='call(this.value)' class='class_company' value="<%= rs.getString("COMPANY") %>"><%= rs.getString("COMPANY") %><br>
<%

//System.out.println("company count:"+i);
i++;
}


}
catch(SQLException e)
{ System.out.println(e.getMessage()); } 

%>
</form>

<script type="text/javascript">

function checkall()
{
        var prmstr = window.location.search.substr(1);
        //alert("string in company.jsp"+prmstr);
        var prmarr = prmstr.split ("&");
        var params = {};

        for ( var i = 0; i < prmarr.length; i++) {
            var tmparr = prmarr[i].split("=");
           // alert("tmparr[0] & tmparr[1] :"+tmparr[0]+" and "+tmparr[1] )
            params[tmparr[0]] = tmparr[1];
        }
         //alert("value to be selected in company.jsp:"+params.company);
        if(params.company=="company1")
            { 

            $('input:radio[value=company1]').attr('checked',true);  

            }
        else
            {
            if(params.company=="company2")
            { 

                $('input:radio[value=company2]').attr('checked',true); 
            }
            else
                {
                if(params.company=="company3")
                    {

                    $('input:radio[value=company3]').attr('checked',true);
                   }
                }       
            }

}
</script>
<script>
window.onload=checkall() ; 
</script>


软件.jsp

<script type="text/javascript">
function stripWhiteSpace(arg){
       if(arg.replace(/^\s+/,'').replace(/\s+$/,'') == ""){
          return true;
       }
    }
function AddUrlParameter(a, b, c)
{
    if (stripWhiteSpace(b))
{
 alert("Parameter name should not be empty.");
 return a;
 } 
    if (stripWhiteSpace(c))
{
  alert("Parameter value should not be empty.");
  return a;
 }
if (a.indexOf("?") == -1)
{
  return a + "?" + b + "=" + c;
}
var d = a.split("?");
if (d.length >= 2)
{

      if (stripWhiteSpace(b))
    // if (d[1].trim() == "")
  {
      return d[0] + "?" + b + "=" + c;
  }
  var e = d[1].split(/[&;]/g);
  for (var f = 0; f < e.length; f++)
  {
    var g = e[f]; var h = g.split("=");
    if (h.length >= 2)
    {
      if (h[0] == b)
      {
           // alert("a :"+a);
            //  alert("b :"+b);
            //alert("c :"+c);
            /*
            if (stripWhiteSpace(b))
          {
            alert("Parameter name should not be empty.");
            return a ;
           }
            if (stripWhiteSpace(c))
          {
            alert("Parameter value should not be empty.");
            return a; 
          }
          var d = "";
          var e = false;
          var f = false;
          if (a.indexOf("?") == -1)
          {
             alert("No Url Parameters Found!");
             return a;
          }
          var g = a.split("?");
          if (g.length >= 2)
          {
            d = d + g[0] + "?";
            var h = g[1].split(/[&;]/g);
            for (var i = 0; i < h.length; i++)
            {
              var j = h[i];
              var k = j.split("=");
              if (k.length >= 2)
              {
                if (k[0] == b)
                {
                 f = true; k[1] = c;
                 d = d + b + "=" + c + "&";
                 }
                else 
                {
                    d = d + j + "&";
                }
                e = true;
              }
             }
            if (f == false)
            {
              alert("Requested query string not found to remove");
              return a;
             }
            if (e == true)
            {
              d = d.slice(0, d.length - 1);
            }
            return d; 
            }

          */
          alert("Url Parameter with provided name already exists! Try Updating that Url Parameter.");

          //UpdateUrlParameter(a, b, c);
          //return a;
       }
    }
   }
  return a + "&" + b + "=" + c;
  }
} 

</script>
<script type="text/javascript">
/*
function callsoft(a) {

    alert("hi");

    var id=document.getElementById("software");
    alert("id :"+id+" value"+a.value);

var $soft = $('input[name=soft]:checked'); 
      var softvalue = $soft.val();
      var id = $soft.attr('id');

      alert("radio clicked id:"+id+" and value: "+softvalue);

      var prmstr = window.location.search.substr(1);
        var prmarr = prmstr.split ("&");
        var params = {};

        for ( var i = 0; i < prmarr.length; i++) {
            var tmparr = prmarr[i].split("=");
              params[tmparr[0]] = tmparr[1];
        }
            var company=params.company;

    var companyurl="main.jsp?company="+company;
    alert(companyurl);

    var new_url = AddUrlParameter(companyurl,id,softvalue);

//var new_url = AddUrlParameter(window.location.href,id,softvalue);
window.location.replace(new_url);

*/


$(document).ready(function(){

    $('.class_sofware').click(function() {


        var $soft = $('input[name=soft]:checked'); 
              var softvalue = $soft.val();
              var id = $soft.attr('id');

              alert("radio clicked id:"+id+" and value: "+softvalue);

              var prmstr = window.location.search.substr(1);
            var prmarr = prmstr.split ("&");
            var params = {};

            for ( var i = 0; i < prmarr.length; i++) {
                var tmparr = prmarr[i].split("=");
                  params[tmparr[0]] = tmparr[1];
            }

            var company=params.company;

            var companyurl="main.jsp?company="+company;

            var new_url = AddUrlParameter(companyurl,id,softvalue);

       //var new_url = AddUrlParameter(window.location.href,id,softvalue);
       window.location.replace(new_url);


    });
});


</script>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<div id="i_all_sw">
<%
Connection conn = null;
Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","","");
ResultSet rs = null;
Statement st=null;
st=con.createStatement();
%>

<%
String S_COMPANY=request.getParameter("company");
if(S_COMPANY!=null){
System.out.println(S_COMPANY);
}
%>
<%
try{
rs = st.executeQuery("select DISTINCT(SW_NAME) from maindatabase where COMPANY='"+S_COMPANY+"'");

int i=0; 
while(rs.next())
  {
%>
<input type="radio" name="soft" id="software" onclick="callsoft(this);" class="class_sofware" value="<%= rs.getString("SW_NAME") %>"><%= rs.getString("SW_NAME") %><br>
<%

//System.out.println("company count:"+i);
i++;
}


}
catch(SQLException e)
{ System.out.println(e.getMessage()); } 

%>
</div>
<script type="text/javascript">

function checkall()
{
        var prmstr = window.location.search.substr(1);
        //alert("string in software.jsp"+prmstr);
        var prmarr = prmstr.split ("&");
        var params = {};

        for ( var i = 0; i < prmarr.length; i++) {
            var tmparr = prmarr[i].split("=");
              params[tmparr[0]] = tmparr[1];
        }
        //alert("params.software value to be selected in software.jsp:"+params.software);
        $('input:radio[value='+params.software+']').attr('checked',true);  


}
</script>
<script>
window.onload=checkall();  
</script>
4

1 回答 1

1

main.jsp做以下编码

<form name="myForm" id="myFormId"
 action="/myServlet" method="POST">

 1. include company.jsp  
 2. include software.jsp
 3. include location.jsp
 4. include dept.jsp

<input type="submit" value="submit">
</form>

注意:单选按钮的名称应company.jspSoftware.jsp

当输出将被呈现给浏览器时,将只有一个表单元素,而在表单元素内部,将根据包含的 JSP 有单选按钮和复选框。

于 2013-03-06T05:05:47.523 回答