2014-01-19 134 views
0

讓一個單選按鈕,說我有2個網頁A和B在網頁AI有這個標籤如何選擇通過鏈接引用

<a href="B.php?var=$somevalue"> Go to B </a> 

在網頁B,我一個單選按鈕列表

 <div class="tabGroup"> 
     <input type="radio" name="tabGroup1" id="view1" class="tab1" checked="checked"/> 
     <label for="view1">View 1</label> 

     <input type="radio" name="tabGroup1" id="view2" class="tab2"/> 
     <label for="view2">View 2</label> 

     <input type="radio" name="tabGroup1" id="view3" class="tab3"/> 
     <label for="view3">View 3</label> 
     </div> 

所以默認選定的單選按鈕是「」view1「,因爲它是」檢查「。但我想要的是,鏈接引用可以檢查單選按鈕」view2「或」view3「取決於$ var我傳遞通過網址。任何幫助將不勝感激。謝謝

+0

請問'var'包含什麼樣的價值? 'view1','view2','view3'? –

+0

它可以包含如下所示的整數值:1,2,3,因此您可以使用它來轉到頁面B上的相應輸入。其實,無論你想要什麼樣的價值,對你來說都很方便 – Nexus

回答

0

那麼,它不是最好的方式,但它很簡單的,如果你只有3個輸入:

<div class="tabGroup"> 
    <input type="radio" name="tabGroup1" id="view1" class="tab1" <?php if($_GET['var'] == 'view1') {echo 'checked="checked"'}?> /> 
    <label for="view1">View 1</label> 

    <input type="radio" name="tabGroup1" id="view2" class="tab2" <?php if($_GET['var'] == 'view2') {echo 'checked="checked"'}?> /> 
    <label for="view2">View 2</label> 

    <input type="radio" name="tabGroup1" id="view3" class="tab3" <?php if($_GET['var'] == 'view3') {echo 'checked="checked"'}?> /> 
    <label for="view3">View 3</label> 
    </div> 
+0

如何對待我們的「檢查」。我仍然希望那樣。它會將view1視爲默認嗎? – Nexus

+0

檢查其他答案,其長,但它更好 – 2014-01-19 08:41:16

0

我認爲這些代碼應該做你所需要的。

$(document).ready(function(){ 
    // get current url 
    var href = location.href; 

    // get the variable value 
    var id_value = href.split('?var=')[1]; 

    // make the radio button checked 
    $('#' + id_value).prop('checked', true); 
}) 

但我認爲一點改善可以使它更美麗。

你應該給每個無線電標籤一個值。

<div class="tabGroup"> 
    <input type="radio" name="tabGroup1" value="1" id="view1" class="tab1" checked="checked"/> 
    <label for="view1">View 1</label> 

    <input type="radio" name="tabGroup1" value="2" id="view2" class="tab2"/> 
    <label for="view2">View 2</label> 

    <input type="radio" name="tabGroup1" value="3" id="view3" class="tab3"/> 
    <label for="view3">View 3</label> 
    </div> 

然後使用下面的代碼

$(document).ready(function(){ 
    // get current url 
    var href = location.href; 

    // get the variable value 
    var checked_radio_value = href.split('?var=')[1]; 

    // make the radio button checked 
    $('input[name="tabGroup1"][value="' + checked_radio_value + '"]').prop('checked', true); 
})