簡體   English   中英

如何使用選擇選項值和Java ID更改2個隱藏的輸入值

[英]How to change 2 hidden input value with select option value and ID with Javascript

我需要根據從下拉菜單中選擇的選項來更改兩個不同的隱藏輸入值。 一個輸入值需要所選選項的值。 (這正在工作。)第二個隱藏的輸入值需要所選選項的ID。 (這不起作用。)我在尋找解決方案,但沒有找到答案。

    <form name="cookbook" id="giftform" action="giftcard.php" onsubmit="redirectOutput(this)">
    <div class="productSelectInput">
    <p>Select your combination of cookbook and gift card amount to buy now.
    <select onchange="GetSelectedValue(this)" id="cookbooks">
        <option id="Giftcard25" value="43.25" selected="selected">Signed Uchi Cookbook & $25 gift card. Price: $43.25</option>
        <option id="Giftcard50" value="93.25">Signed Uchi Cookbook & $50 Gift Card. Price: $93.25</option>
        <option id="Giftcard75" value="118.25">Signed Uchi Cookbook & $75 Gift Card. Price: $118.25</option>
        <option id="Giftcard100" value="143.25">Signed Uchi Cookbook & $100 Gift Card. Price: $143.25</option>
        <option id="Giftcard150" value="193.25">Signed Uchi Cookbook & $150 Gift Card. Price: $193.25</option>
        <option id="Giftcard200" value="243.25">Signed Uchi Cookbook & $200 Gift Card. Price: $243.25</option>
        <option id="Giftcard250" value="293.25">Signed Uchi Cookbook & $250 Gift Card. Price: $293.25</option>
    </select>
    </p>
    <p>If you are sending this gift directly to your recipient, you may enter a gift message here (please include sender's name, if applicable)
    <textarea cols="43" rows="3" name="giftmessage" class="giftmessage"></textarea>
    </p>
    </div>
    <input type="hidden" name="description" id="description" value="" />
    <input type="hidden" name="amount" id="amount" value="43.25" />
    <input type="submit" value="Buy Now" class="simple-input" />
</form>
<script type="text/javascript">
    function GetSelectedValue(cookbooks) {
        var selectedValue = cookbooks.value;
        document.cookbook.amount.value = selectedValue;
        var selectedMy = document.getElementById("cookbooks").id;
        document.getElementById("typegc") = selectedMy;

    }
</script>

我不知道哪個值去哪里(取決於您),但是您錯過的是獲取所選選項的值,這是代碼:

 function GetSelectedValue(cookbooks) {
        var selectedValue = cookbooks.options[cookbooks.selectedIndex].value;
        var selectedMy = cookbooks.options[cookbooks.selectedIndex].id;
        document.getElementById("description").value = selectedValue;
        document.getElementById("amount").value = selectedMy;

     //You can erase this, just for demonstration to have the correct values
     console.log(selectedValue);
     console.log(selectedMy);
    }

希望能有所幫助。

以下更改將有助於解決您的問題:

 function GetSelectedValue(cookbooks) {
        var selectedValue = cookbooks.value;
        document.cookbook.amount.value = selectedValue;
        var selectedId = cookbooks[cookbooks.selectedIndex].id;

    }

試試這個場景。

希望對您有所幫助

暫無
暫無

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

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