0

下面是我在 jsp 中用于创建选项卡的 html 代码。

<form name="form1" action="/SampleServlet" method="post">
<ul id="navigation">
<li><a id="tab1" name="live" class="selected" onclick="parent.frame1.location='frame1.jsp'">LIVE</a></li>
<li><a id="tab2" name="history" onclick="parent.frame1.location='frame2.jsp'">HISTORY</a></li>
</ul>

现在我想在我的 servlet 中选择选项卡或激活的选项卡名称或 ID。我该如何使用

request.getParameter("") 

获取选定或激活选项卡的方法?请帮助我。我找到了一些使用 jquery 的解决方案,但我不想使用 Jquery。请帮助我。如何获取当前选择的选项卡名称或 id 或值或目前使用 request.getParameter() 激活?

4

3 回答 3

0

使用 jQuery!它会让你的工作更轻松!只需在这些选项卡上注册一个点击处理程序,然后向 servlet 发送一个 ajax 请求,参数为您的选项卡 ID,如此简单!下面是代码:

假设:Tab1:ID='tab1',CLASS='tab' Tab2:ID='tab2',CLASS='tab'

// click handler
$('.tab').click(function(){
  // get tab id
  var id = $(this).attr('id');

  // now send an ajax request to servlet with parameter which stores this id value
  $.ajax({
    type: 'GET',  // request type
    data: {param: id},
    url: '/servleturl',
    dataType: 'text',  // or json or any other which your servlet will return

    // this will be executed when request is successful and a correct response is recieved
    success: function(response){
      alert(response); // or do whatever you like to do with response
    },

    // this function will be executed when there is any error
    error: function(){

    },

    // this will be always executed, like a finally block in Java
    complete: function(){

    },
  });
});

您可以省略错误并完成功能;我已经告诉了你所有必要的东西。就是这么简单。

为了在 servlet 中接收 ID,您可以使用:

String tabId = request.getParameter('param');

'param',因为我已经指定:

data: {param: id}

如果您使用“param”以外的任何其他名称,请改用该名称。

为了从 servlet 发送响应,只需使用:

PrintWriter out = response.getWriter();
out.write("ABCD or whatever you want");
out.close();  // DON'T forget this!
于 2013-06-26T07:43:17.257 回答
0

请确保您没有使用重复的 id 例如:Change second link id to tab2

<ul id="navigation">
<li><a id="tab1" name="live" class="selected" onclick="parent.frame1.location='frame1.jsp'">LIVE</a></li>
<li><a id="tab2" name="live" class="selected" onclick="parent.frame1.location='frame2.jsp'">HISTORY</a></li>
</ul>

你尝试使用request.getparametervalues()

于 2013-06-26T07:34:39.263 回答
0

如果没有 jQuery,将很难选择它,您必须迭代所有a元素才能找到它(或者需要其他一些迭代)。

  1. 您必须找到a带有“已选择”的类并读取它的 ID($("a.selected").attr('id')在 jQuery 中)。核心 JS 中没有“getElementByClass”...

  2. 在这两种情况下,您都必须将此值设置为隐藏的表单元素,必须将其添加到<form>以下内容:<input type="hidden" name="activetab"></input>

  3. 然后你会在 servlet 中找到它request.getParameter("activetab")

无论如何,这是一个核心 Javascript 解决方案:http: //jsfiddle.net/balintbako/NdNqX/

<script type="text/javascript">
    function setTabAndSubmit() {
        var tab;
        var anchors = document.getElementsByTagName("a");
        for (var i = 0; i < anchors.length; i++) {
            if (anchors[i].className == 'selected') {
                tab = anchors[i].id;
                break;
            }
        }
        document.forms["myform"].elements["activetab"].value = tab;
        alert(tab);
    }
</script>
<form name="myform" action="/path" onSubmit="setTabAndSubmit()">
    <input name="activetab" type="hidden"></input>
    <input name="somefield" type="text"></input>
    <input type="submit" value="Submit"></input>
</form> 
<a id="a1" href="#" class="selected">link 1</a>
<a id="a2" href="#">link 2</a>
于 2013-06-26T07:44:07.650 回答