【问题标题】:How do i make a radio button link to a page using a button如何使用按钮将单选按钮链接到页面
【发布时间】:2020-05-06 16:08:20
【问题描述】:

我如何制作一个单选按钮,它可以链接到一个页面,每个单选按钮都链接到一个网站,当我按下一个按钮时,我希望它引导我到选定的单选按钮链接,我不希望它be onclick 我希望它在我按下按钮时引导我。我试过这样做,但我需要帮助。 这是我尝试过的:

    <h3>
Which part of the building do you want cleaning
</h3>
<form name="frmSite" style="float: left; margin-top: 5px;"> 
  <a class="loginPg_choices" href="www.youtube.com">
    <input type="radio" name="site" value="1" id="radio_btn_1" style="float: left; clear: both;" />
    <span style="float: left; margin: -2px 0 0 5px;">The whole building</span>
    <br/> 
  </a>
  <a class="loginPg_choices" href="www.google.com">
    <input type="radio" name="site" value="1" id="radio_btn_1" style="float: left; clear: both;" />
    <span style="float: left; margin: -2px 0 0 5px;">Kithcen</span>
    <br/> 
  </a>
  <a href="#" type="submit" value="Submit" title="Login" style="float: right; clear: both;">
    Submit
  </a>
</form>

这是我尝试过的https://jsfiddle.net/nb7jmfxp/

【问题讨论】:

    标签: html button radio-button


    【解决方案1】:

    Here 是字段集文档。

    HTML代码:

    <form>
      <fieldset>
        <legend>Choose your Website</legend>
    
        <input type="radio" id="youtube" name="monster">
        <label for="youtube">Youtube</label><br />
    
        <input type="radio" id="google" name="monster">
        <label for="google">Google</label><br />
    
      </fieldset>
    </form>
    <button id="go_to_website">
      Go to website
    </button>
    
    

    js代码:

      $("#go_to_website").click(function(event) {
        var link_id = $("input[name='monster']:checked").attr("id");
        if (link_id) {
          switch (link_id) {
            case "youtube":
              // code block
              window.location.href = 'www.youtube.com';
              break;
            case "google":
              // code block
              window.location.href = 'www.google.com';
              break;
            default:
              // code block
          }
        } else {
          alert("please pick an option")
        }
      })
    

    Jsfiddle 链接:https://jsfiddle.net/pc62oL74/4/

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-21
      • 1970-01-01
      • 2020-09-26
      • 2019-06-10
      • 2013-02-13
      相关资源
      最近更新 更多