javascript - 使用 switch 语句选择单选按钮时更改 href 值

标签 javascript jquery input switch-statement href

尝试根据选择的单选按钮更改语句中的 href 引用

        <ul class="size_selector">
                        <li class="top"><input type="radio" class="radioselect" name="id" id="test1" value="test1" /><label for="test1">1</label></li>
                        <li class="top"><input type="radio" class="radioselect" name="id" id="test2" value="test2"/> <label for="test2">2</label></li>
                        <li class="top"><input type="radio" class="radioselect" name="id" id="test3" value="test3"/> <label for="test3">3</label></li>
                        <li class="top"><input type="radio" class="radioselect" name="id" id="test4" value="test4"/> <label for="test4">4</label></li>
        </ul>
<a id="shopCart" href="#">
    Take me to your leader
</a>

查询

$("input[type='radio']").change(function(){

switch($(this).val()) {
  case "test1" :
    document.getElementById("shopCart").href="http://yahoo.com"; 
    break;
  case "test2" :
    document.getElementById("shopCart").href="http://bing.com"; 
    break;
  case "test3" :
    document.getElementById("shopCart").href="http://google.com"; 
     break;
  case "test4" :    document.getElementById("shopCart").href="http://stackoverflow.com";
    break;
  };

});

http://codepen.io/homogenizer/pen/xOrzgZ

最佳答案

您正在使用 JQuery,但没有将其链接进来。
设置 -> JavaScript
https://code.jquery.com/jquery-3.1.0.min.js 中添加此链接.

关于javascript - 使用 switch 语句选择单选按钮时更改 href 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38947982/

相关文章:

python - 如果 python 出现错误,请用户重新输入

进行公差分析的代码

javascript - 承载授权,来自服务器的 400 错误

javascript - 不要在 ajax laravel 5.2 中运行 window.location.href

javascript - Jquery 验证未应用于字段

jquery - 如何在 JQuery 中向 ui-dialog-titlebar 添加图像?

c - 读取用户的端口号

javascript - fbAlbum2.js、bootstrap-tooltip 和 Magnific-Popup

多个按钮上的 javascript 函数

javascript - 函数覆盖、onload 优先