简体   繁体   English

根据单选按钮选择更改值和链接

[英]Change value and link depending on radio button selection

I'm trying to get the text value and the link to change depending on which radio button is selected.我正在尝试获取文本值和链接以根据选择的单选按钮进行更改。 For example, selecting the first radio button would change the text to "Google" and it would link to google.com.例如,选择第一个单选按钮会将文本更改为“Google”并链接到 google.com。 While selecting the second radio button would change the text to "bing" and link to bing.com.选择第二个单选按钮会将文本更改为“bing”并链接到 bing.com。 I have managed to get the text to change, however the link is not working correctly.我已设法更改文本,但是链接无法正常工作。 Any help would be appreciated.任何帮助,将不胜感激。

jsfiddle - https://jsfiddle.net/3yrcusv8/ jsfiddle - https://jsfiddle.net/3yrcusv8/

 $('.radiogroup').change(function(e){ var selectedValue = $(this).val(); $('#amount').val(selectedValue) });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="radio" name="radiogroup" id="radiogroup1" class="radiogroup" value="Google" /> <input type="radio" name="radiogroup" id="radiogroup2" class="radiogroup" value="Bing" /> <input type="submit" name="amount" id="amount" onclick='myFunction()' /> <script> function myFunction() { if(document.getElementById('radiogroup1').checked) { document.getElementById('amount').href = "https://google.com"; } } </script>

Explanation解释

Okay, so rather than having a large if statement, or a large switch statement, you can set a HTML attribute to contain the relevant URL, in my answer, you can see that I've used data-url .好的,与其使用大的 if 语句或大的 switch 语句,不如设置一个 HTML 属性来包含相关的 URL,在我的回答中,您可以看到我使用了data-url Personally I find this cleaner for the JavaScript side of things, it also means less logic to be processed within the JavaScript run time too.就我个人而言,我发现这对于 JavaScript 方面来说更干净,这也意味着在 JavaScript 运行时处理的逻辑更少。

Considering with the change function, you reference the radio button that has been clicked, you can just access the data-url attribute from there, and just update the a tag.考虑到change功能,您引用已单击的单选按钮,您可以从那里访问data-url属性,然后更新a标签。

Another note, using an a tag over a button/input tag makes more sense as an a tags purpose is pretty much to be a link on a page, I'd imagine using your original approach may cause some bugs, especially in specific browsers, at least with an a tag, you know it'll work.另外要注意,使用a标签上的按钮/输入标签更有意义作为a标签的目的是几乎成为一个页面上的链接,我想像用你原来的做法可能会导致一些错误,尤其是在特定的浏览器,至少有a标签,你知道它会起作用。

 $('.radiogroup').change(function(e) { const $this = $(this), $link = $("#url"); $link.html($this.val()); $link.attr("href", $this.attr("data-url")); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="radio" name="radiogroup" id="radiogroup1" class="radiogroup" value="Google" data-url="https://google.com" /> <input type="radio" name="radiogroup" id="radiogroup2" class="radiogroup" value="Bing" data-url="https://www.bing.com/" /> <a id="url" href="" target="_blank">null</a>

Edit编辑

This solution simply uses native JavaScript, nothing else.此解决方案仅使用本机 JavaScript,仅此而已。

 const a = document.getElementById("url"); const inputs = document.querySelectorAll('.radiogroup'); // A simple function to handle the click event for each input. const clickHandler = i => { a.href = i.getAttribute("data-url"); a.textContent = i.getAttribute("value"); }; // Possibly even less code again. inputs.forEach(i => i.onclick = () => clickHandler(i));
 <input type="radio" name="radiogroup" id="radiogroup1" class="radiogroup" value="Google" data-url="https://google.com" /> <input type="radio" name="radiogroup" id="radiogroup2" class="radiogroup" value="Bing" data-url="https://www.bing.com/" /> <a id="url" href="" target="_blank">null</a>

Not sure why you try to change the href of the submit input, the input doesn't take href as an attribute and it won't do anything if it was changed as well.不知道您为什么要尝试更改提交输入的 href,输入不会将 href 作为属性,并且如果更改也不会执行任何操作。

Simply redirect the user when they click the button inside the function.只需在用户单击函数内的按钮时重定向用户即可。

function myFunction() {
    if(document.getElementById('radiogroup1').checked) {
        window.location.replace("http://google.com");
    } else {
      window.location.replace("http://bing.com");
    }
}

You can use custom attributes to pass the URL around as below, if you want the user to click the button to open the page.如果您希望用户单击按钮打开页面,您可以使用自定义属性来传递 URL,如下所示。

Google refuses the connection from their end, but you can see from the Bing option that the code works.谷歌从他们的一端拒绝连接,但你可以从必应选项中看到代码有效。

 // Add click event to radiogroup $('.radiogroup').change(function(e) { // Update url attribute and value of button from radio button $('#amount').attr("url", $(this).attr("url")).val($(this).val()); }); // Add click event to submit button $(document).on("click", "#amount", function() { // Print URL to console (Can be deleted) console.log( $(this).attr("url") ); // Open page in window window.location.assign($(this).attr("url")); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="radio" name="radiogroup" id="radiogroup1" class="radiogroup" value="Google" url="http://google.com"/> Google <input type="radio" name="radiogroup" id="radiogroup2" class="radiogroup" value="Bing" url="http://bing.com" /> Bing (working) <input type="submit" name="amount" id="amount" />

We can use a dynamic function to pull this off.我们可以使用动态函数来实现这一点。

You can simply make a dynamic function in JS that will work for n number of radial buttons by fetching their value attribute and id and change the input button accordingly.您可以简单地在 JS 中创建一个动态函数,通过获取它们的 value 属性和 id 并相应地更改输入按钮,该函数适用于 n 个径向按钮。

The code snippet:代码片段:

 <input id="google" type="radio" name="radiogroup" class="radiogroup" value="Google" onclick="changeLink(event)" /> <input id="bing" type="radio" name="radiogroup" class="radiogroup" value="Bing" onclick="changeLink(event)" /> <input type="submit" name="amount" id="btn" /> <script> const changeLink = e => { let _target = e.currentTarget.id, //get the id name. targetValue = document.getElementById(_target).getAttribute('value'), //use id name to fetch value attribute. btn = document.getElementById('btn'); //get the btn element from DOM. btn.setAttribute('value', targetValue); //set btn elements 'src' attribute to the value of the radial button clicked. btn.setAttribute('src', 'https://' + targetValue.toLowerCase() + '.com'); //same src but convert to lower case first. console.log(btn); //for testing the link } </script>

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

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