簡體   English   中英

如何從一個html頁面獲取單選按鈕的價值?

[英]How to get value of radio buttons from one html page to another?

我嘗試獲取位於第一個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>

我不確定您要尋找哪種解決方案。 但是,如果您不想使用更深入的知識,例如php中的$ _SESSION變量,則可能正在尋找像cookie之類的東西。 是關於Cookie的精彩網頁。

還可以從htmlgoodies.com查看教程,了解有關通過javascript進行變量傳遞的其他一些想法。

如果您對Cookie沒問題,則可以使用已有的東西,只需清理一下jQuery。

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

小提琴

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM