Javascript 如何通过链接参考选择单选按钮

Javascript 如何通过链接参考选择单选按钮,javascript,php,html,Javascript,Php,Html,假设我有两个网页A和B。在网页A上我有这个标签 <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

假设我有两个网页A和B。在网页A上我有这个标签

<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>

视图1
视图2
视图3

因此,默认选中的单选按钮是“view1”,因为它是“选中的”。但我想要的是,链接引用可以为我检查单选按钮“view2”或“view3”,这取决于我通过的$var URL。如果您有任何帮助,我们将不胜感激。谢谢,这不是最好的方法,但如果您只有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>

/>
视图2

我认为这些代码应该满足你的需要

$(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>

var
包含什么值?
view1
view2
view3
?它可以包含整型值,如:1、2、3,因此您可以使用这些值转到B页上的相应输入。实际上,无论您想要什么值,都很方便。检查的“defaut”值如何”“我还是想要。它会使view1成为一个defaut吗?检查另一个答案,它很长,但要好得多
$(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);
})