【问题标题】:How to get value of radio buttons from one html page to another?如何从一个 html 页面获取单选按钮的值到另一个页面?
【发布时间】:2014-10-27 03:16:35
【问题描述】:

我尝试获取位于第一个 html 页面上的底部粘贴的 html 代码的值。我有另一个 html 页面,应该使用“onclick”按钮生成。更准确地说,代码应该在下一页中创建按钮。

案例 1: 选择了 radio-choice-1 - 它应该创建 4 个按钮。

案例 2: 选择了 radio-choice-2 - 它应该创建 4 或 9 个按钮。

案例 3: 选择了 radio-choice-3 - 它应该创建 9 或 16 个按钮。

案例 4: 选择了 radio-choice-4 - 它应该创建 16 个按钮。

我不知道如何获取所选单选按钮的值,然后在另一个页面上生成按钮。

我其实有一点脚本(game.js):

$(document).ready(function(){
    $('#radio-button-value').click(function(){
        $("input[name='radio-button-gruppe']:checked".val());
    });
});

第一个 html 页面

     <html>
    <head>
        <meta charset="utf-8" />
        <title>newgame</title>

        <link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css" />
    <script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
    <script src="http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.js"></script>

    <!-- Einstellungen zur Defintion als WebApp -->
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

    <script src="game.js"></script> 
    </head>
    <body>


    <div data-role="main" class="ui-content">
    <form>
    <fieldset data-role="controlgroup">
  <legend>Choose a mode:</legend>
  <input type="radio" name="radio-choice" id="radio-choice-1" value="choice-1" checked="checked">
  <label for="radio-choice-1">easy</label>

  <input type="radio" name="radio-choice" id="radio-choice-2" value="choice-2">
  <label for="radio-choice-2">medium</label>

  <input type="radio" name="radio-choice" id="radio-choice-3" value="choice-3">
  <label for="radio-choice-3">difficult</label>

  <input type="radio" name="radio-choice" id="radio-choice-4" value="choice-4">
  <label for="radio-choice-4">hard</label>
</fieldset>
<input type="button" id="radio-button-value" name="game" value="create game" class="button" data-theme="b"/>
</form>





  </div>

  <div data-role="footer">

  </div>
</div> 
    </body>
</html>

【问题讨论】:

  • 按照以下问题的建议使用 cookie。这就是饼干的用途。存储数据位。

标签: javascript jquery html jquery-mobile web-applications


【解决方案1】:

我不确定您正在寻找哪种解决方案。但是如果你不想使用更根深蒂固的东西,比如 php 中的 $_SESSION 变量,你可能正在寻找像 cookie 这样的东西。 Here 是一个很棒的关于 cookie 的页面。

还可以查看来自 htmlgoodies.com 的 this 教程,了解使用 javascript 进行变量传递的其他一些想法。

如果您对 cookie 没问题,您几乎可以使用已有的,只需稍微清理一下 jquery。

$(document).ready(function(){
    $('#radio-button-value').click(function(){
        var difficulty = $("input[name='radio-choice']:checked").val();
        document.cookie = "gameDifficulty=" + difficulty + ";path=/";
    });
});

fiddle

【讨论】:

  • 当我在 php 中使用 session 时,如何设置值并稍后获取值?
猜你喜欢
  • 2013-12-15
  • 2021-08-19
  • 1970-01-01
  • 2015-11-24
  • 1970-01-01
  • 2016-04-04
  • 1970-01-01
  • 1970-01-01
  • 2011-01-26
相关资源
最近更新 更多