简体   繁体   English

单击标签并将数据传递到URL时如何选择单选按钮?

[英]How to Select Radio Button When Clicking Label & Pass Data to URL?

I would like to be able to select a radio button when clicking on "label" and then pass that value to the URL. 我希望能够在单击“标签”时选择一个单选按钮,然后将该值传递给URL。

Here's the URL Prefill code below. 这是下面的URL预填充代码。

It works if you select the actual radio button. 如果选择实际的单选按钮,它将起作用。 However, I realized that if you click on label it doesn't select the radio button. 但是,我意识到,如果单击标签,则不会选择单选按钮。

So i added this top part jquery code to solve that issue. 因此,我添加了此顶级jquery代码来解决该问题。 However, it doesn't pass the value to the URL. 但是,它不会将值传递给URL。

I'd like to stylize radio buttons by hiding input part and adding pseudoclasses. 我想通过隐藏输入部分并添加伪类来样式化单选按钮。 so i wonder if there's a way to pass radio button value when label is clicked on? 所以我想知道是否有一种方法可以在单击标签时传递单选按钮值?

 $(function(){ $('li').click(function(){$(this).find('input[type=radio]').prop('checked', true);}); }); /* URL Prefill Code */ var form = document.getElementById('form-product-choices'); form.addEventListener('change', function(e){ var actionUrl = 'https://example.com/'; var radioButtons = document.getElementsByName('product-choice'); for( var i = 0; i < radioButtons.length; i++ ) { if( radioButtons[i].checked ) { actionUrl += '?product-choice=' + radioButtons[i].value; break; } } document.getElementById('action-link').href = actionUrl; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <form id='form-product-choices' action=''> <main role="main"> <section id="p1"> <ul> <li><label for="radio"><input type='radio' name='product-choice' value='regular' /> regular</label></li> <li><label for="radio"><input type='radio' name='product-choice' value='double' /> double</label></li> <li><label for="radio"><input type='radio' name='product-choice' value='max-xl' /> xl</label></li> </ul> <a class="quizbut">Next</a> </section> <section id="p4"> <a id='action-link' class='quizbut' href=''>Click to continue</a> </section> </main> </form> 

First of all, we don't need JQuery code for select the radio button when clicking the label. 首先,单击标签时,我们不需要JQuery代码来选择单选按钮。 Instead of this you can able to use below code, 除此之外,您可以使用以下代码,

<label for="regular">
  <input type='radio' name='product-choice' value='regular' id="regular" /> regular
</label>

For achieving this, Need to set the ID attribute in textbox and use that ID attribute value on label 'for' attribute. 为此,需要在文本框中设置ID属性,并在标签“ for”属性上使用该ID属性值。

Below, I have added a full code. 在下面,我添加了完整的代码。 Hope this will help. 希望这会有所帮助。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>

<form id='form-product-choices' action=''>
    <main role="main">
        <section id="p1">
            <ul>
                <li>
                    <label for="regular">
                        <input type='radio' name='product-choice' value='regular' id="regular" /> regular
                    </label>
                </li>
                <li>
                    <label for="double">
                        <input type='radio' name='product-choice' value='double' id="double" /> double
                    </label>
                </li>
                <li>
                    <label for="xl">
                        <input type='radio' name='product-choice' value='max-xl' id="xl"  /> xl
                    </label>
                </li>
            </ul>
            <a class="quizbut">Next</a>
        </section>

        <section id="p4">
            <a id='action-link' class='quizbut' href='javascript:void(0)'>Click to continue</a>
        </section>
    </main>
</form>

<script type="text/javascript">
    $("#action-link").click(function(){
        var getVal = $('input[name=product-choice]:checked').val();
        window.location.href='https://example.com/test.cfm?product-choice='+getVal;
    });
</script>

On the label, associate the for attribute with a corresponding id on each of the radio buttons. 在标签上,将for属性与每个单选按钮上的对应id关联。 The ids should be unique between them. 这些ID在它们之间应该是唯一的。

 $(function(){ $('li').click(function(){$(this).find('input[type=radio]').prop('checked', true);}); }); /* URL Prefill Code */ var form = document.getElementById('form-product-choices'); form.addEventListener('change', function(e){ var actionUrl = 'https://example.com/'; var radioButtons = document.getElementsByName('product-choice'); for( var i = 0; i < radioButtons.length; i++ ) { if( radioButtons[i].checked ) { actionUrl += '?product-choice=' + radioButtons[i].value; break; } } document.getElementById('action-link').href = actionUrl; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> <form id='form-product-choices' action=''> <main role="main"> <section id="p1"> <ul> <li><label for="regular"><input id="regular" type='radio' name='product-choice' value='regular' /> regular</label></li> <li><label for="double"><input id="double" type='radio' name='product-choice' value='double' /> double</label></li> <li><label for="max"><input id="max" type='radio' name='product-choice' value='max-xl' /> xl</label></li> </ul> <a class="quizbut">Next</a> </section> <section id="p4"> <a id='action-link' class='quizbut' href=''>Click to continue</a> </section> </main> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM