2014-01-19 129 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); 
})