2013-06-26 173 views
0

以下是我在jsp中用于创建选项卡的html代码。如何获得使用java的活动标签名称或ID?

<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.How所选标签或标签被激活名称或ID,我可以使用

request.getParameter("") 

方法获得选择或激活的标签?请帮助我,我发现了一些解决方案使用jquery,但我不想使用jquery.Please help me.How can get the tab name or id or value which are selected to present or activated currently now using request.getParameter()?

+0

Java或JavaScript的? – Thihara

+0

你怎么调用你的servlet? –

+0

为什么你不想使用jQuery? – kunal18

回答

0

请确保您没有使用重复比如:id:更改第二链路ID来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()

+0

请看我编辑的代码.. – user2515189

+0

request.getParameter()应该采取输入参数是否正确?那我应该怎么做request.getParameter(“id”);如果我选择tab2,它不会给出结果。 – user2515189

+1

更好用jQuery >> jQuery(“。selected”)。click(function(){ }); – gjman2

0

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

假设:TAB1:ID = 'TAB1',CLASS = '标签' TAB2:ID = 'TAB2',CLASS = '标签'

// 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! 
0

这将是很难选择它没有jQuery的,你必须遍历所有a元素找到它(或其他一些迭代会必要)。

  1. 你必须找到a与“选择”级和读取它的ID(这是jQuery的$("a.selected").attr('id'))。没有“getElementByClass”核心JS ...

  2. 在你有这个值设置为一个隐藏的表单元素,它已被添加到<form>如下两种情况:

  3. 而且那么你会在request.getParameter("activetab")的servlet中找到它。

反正这是一个核心的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> 
+0

我没有得到你。请解释我。什么是“你必须选择一个与”选择“类并阅读它的ID”?请帮我 – user2515189

+0

为什么要用这个东西,用隐藏的元素,所有使用jQUery这个东西都是小菜一碟!我给你的代码似乎对你来说很难?你对JavaScript是否舒服? – kunal18

+0

我改为措辞:选择意味着你必须找到一个“CSS选择器” –

相关问题