javascript
  • jquery
  • jsp
  • struts2
  • radio-button
  • 2014-05-20 33 views 0 likes 
    0

    我得到由環在JSP推一些單選按鈕:如何使用單選按鈕更改部分網址(ID)?

    <input type="radio" name="radioBtn" value="<s:property value="#country.getIdCountry()"/>"> 
    

    的單選按鈕的值描述它選擇一個國家的ID。

    然後,我創建了一些鏈接,例如:

    <a href='addDefaultPoints.action?countryId=<s:property value="#country.getIdCountry()"/>&height=400&width=350'>Link 1</a>   
    <a href='editCountryMap.action?height=550&width=750&id=<s:property value="#country.getIdCountry()"/>&keepThis=true&TB_iframe=true'>Link 2</a> 
    

    我想更改代碼到一些動態頁面,可以改變單選按鈕組的onChange事件的ID,在指定的網址中HREF。

    我嘗試使用Javascript進行此功能,但沒有運氣。也許我需要在服務器端做到這一點。我使用的是JSP,struts,Jquery和Javascript。

    這裏是我的嘗試:

    var myGetValue = parseURL("id"); 
    function checkV(f,v){ 
        for(var i=0;i<c.length;i++){ 
         c[i].checked=(c[i].value==v)?true:false; 
        } 
    } 
    checkV(myForm,myGetValue); 
    
    +0

    你能告訴我們你試過什麼嗎? –

    +0

    發表在最近的編輯 – TheKolanN

    回答

    1

    你可以寫如下的邏輯:

    創建與全縣ID一些常量名隱藏輸入網址,對相應的錨標記

    使用相同的類名
    <input class="COUNTY_ID" type="hidden" value="addDefaultPoints.action?countryId=COUNTY_ID&height=400&width=350"> 
    
    <a class="COUNTY_ID" href="#"/> 
    

    的單選按鈕寫入更改事件:

    $('input[name="radioBtn"]').change(function(){ 
    
        var radioVal = $(this).val(); 
        var url = $('input[class="COUNTY_ID"]').val(); 
    
        url = url.replace('COUNTY_ID',radioVal); 
    
        $('a[class="COUNTY_ID"]').attr('href',url); 
    }); 
    

    使用類似的邏輯,其他環節也...

    +0

    謝謝,我會檢查,並讓你知道很快。 – TheKolanN

    1

    HTML

    <input type="radio" name="radioBtn" value="1"> 
    <input type="radio" name="radioBtn" value="2" checked> 
    <input type="radio" name="radioBtn" value="3"> 
    <input type="radio" name="radioBtn" value="4"> 
    
    <a id="lnk1" href='addDefaultPoints.action?height=400&width=350&countryId='>Link 1</a>   
    <a id="lnk2" href='editCountryMap.action?height=550&width=750&keepThis=true&TB_iframe=true&id='>Link 2</a> 
    

    JS

    $(document).ready(function() { 
        var countryId = $('input[name=radioBtn]:checked').val(); 
    
        $('a#lnk1').attr('href', $('a#lnk1').attr('href') + countryId); 
        $('a#lnk2').attr('href', $('a#lnk2').attr('href') + countryId); 
    }); 
    

    說明

    首先通過檢查檢查得到所選國家該組的單選按鈕radioBtn。 然後你確定你的url的國家參數是最後的,這樣你可以連接countryId到其餘的鏈接屬性'href'。

    結果

    鏈接1:addDefaultPoints.action?height=400&width=350&countryId=2

    鏈路2:editCountryMap.action?height=550&width=750&keepThis=true&TB_iframe=true&id=2

    如果參數是不是廣告中的鏈接的末尾,則可以使用replace()功能類似的Bhushan Kawadkar提及。

    +0

    這看起來很不錯,但是 $('a#lnk1')。attr('href',$('a#lnk1')。attr('href')+ countryId); 不能按預期方式工作。該ID不會在我的href結尾添加。該ID可以從單選按鈕中正確獲取,但是不會附加ID。 – TheKolanN

    +0

    這裏是我的[jsFiddle](http://jsfiddle.net/VDesign/LwXyR/) – Mivaweb

    相關問題