【问题标题】:How do it have a radio button pass data hidden from url?它如何让单选按钮从 url 中隐藏传递数据?
【发布时间】:2015-02-19 10:38:36
【问题描述】:

事情是这样的,我必须展示一个概念证明,其中我:

  • 使用 url 部分在页面之间传递数据并显示在另一个页面中传递的数据。

  • 使用 url 部分在页面之间传递数据并显示在另一个页面中传递的数据。

意思是我希望其中一个单选按钮的行为如下:输入类型=“隐藏”, 但我不知道该怎么做,以澄清下面有一段代码。任何帮助表示赞赏。澄清一下,它不应该在查询字符串中显示单选按钮的值。

<form method="POST" action="Confirm.jsp">


  <p>
    Choose:
    <input type="radio" checked name="QuizType" value="Private">Private
    <input type="radio" name="QuizType" value="Public">Public
    <br>

    <input type="submit" name="submit" value="submit">
  </p>
</form>

【问题讨论】:

    标签: javascript html radio-button hidden-field


    【解决方案1】:

    你的表格:

    <form id="frm1">
    <p>
      Choose:
      <input type="radio" checked name="QuizType" value="Private" id="radio">Private
    
      <input type="radio" name="QuizType" value="Public">Public
    
      <button onclick="myFunction()">Click</button>
    </p>
    </form>
    

    然后就可以用 JavaScript 获取选中的值了:

    function myFunction() {
      var radios = document.getElementsByName('QuizType');
    
      for (var i = 0, length = radios.length; i < length; i++) {
         if (radios[i].checked) {
    
           // Store the value for the selected radio 
           // You can maybe redirected on another page and pass this variable
           $selectedRadio = radios[i].value
    
           alert($selectedRadio);
           break;
         }
       }
    }
    

    检查这个Example

    如果您想将变量传递给 url,那么您应该尝试:

    window.location.href+'/'+$selectedRadio;
    

    资源: Get Radio Button Value with Javascript, Is there a way to pass javascript variables in url?, How to redirect to another webpage in JavaScript/jQuery?

    【讨论】:

    • 谢谢你,尽管我最终决定根据我的需要创建两种表单,一种使用 POST 方法,另一种没有。这样,“公共”会显示查询字符串中的数据,而私有则不会。
    猜你喜欢
    • 2020-01-29
    • 2021-05-11
    • 2014-12-18
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 2014-04-21
    • 2015-10-16
    • 1970-01-01
    相关资源
    最近更新 更多