如何将单选按钮的值从一个 html 页面获取到另一个页面? [英] How to get value of radio buttons from one html page to another?

查看:53
本文介绍了如何将单选按钮的值从一个 html 页面获取到另一个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Case1: radio-choice-1 被选中 - 它应该创建 4 个按钮.

Case2: radio-choice-2 被选中 - 它应该创建 4 或 9 个按钮.

Case3: radio-choice-3 被选中 - 它应该创建 9 或 16 个按钮.

Case4: radio-choice-4 被选中 - 它应该创建 16 个按钮.

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

我实际上有一些脚本(game.js):

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

第一个 html 页面

 <头><meta charset="utf-8"/><title>新游戏</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><身体><div data-role="main" class="ui-content"><表格><fieldset data-role="controlgroup"><legend>选择一种模式:</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">困难</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"/></表单>

<div data-role="footer">

</html>

解决方案

我不确定您正在寻找哪种解决方案.但是如果你不想使用更根深蒂固的东西,比如 php 中的 $_SESSION 变量,你可能正在寻找类似 cookie 的东西.这里是关于 cookie 的精彩页面.

另请查看 htmlgoodies.com 上关于变量传递的教程使用 javascript 获取其他一些想法.

如果你对 cookie 没问题,你几乎可以使用你已经拥有的东西,只需稍微清理一下 jquery.

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

fiddle

I try to get the value of the at the bottom pasted html code who stands on the first html page. I have another html page, which should be generated with the button "onclick". More precisely the code should create buttons in the next page.

Case1: radio-choice-1 is selected - it should create 4 buttons.

Case2: radio-choice-2 is selected - it should create 4 or 9 buttons.

Case3: radio-choice-3 is selected - it should create 9 or 16 buttons.

Case4: radio-choice-4 is selected - it should create 16 buttons.

I dont know how to get the value of the selected radio buttons then generate buttons on another page.

I actually have a bit of script(game.js):

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

first html page

     <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>

解决方案

I'm not sure what sort of solution you're looking for. But if you don't want to use something more deeply rooted, like the $_SESSION variable in php, you're probably looking for something like cookies. Here's a great page on cookies.

Also check out this tutorial from htmlgoodies.com on variable passing with javascript for some other ideas.

If you're okay with cookies, you can pretty much use what you already have, just clean up the jquery a little.

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

See fiddle

这篇关于如何将单选按钮的值从一个 html 页面获取到另一个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
前端开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆