2013-06-26 171 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選擇器」 –

相關問題