2015-11-05 66 views
0

我需要根据从下拉列表中选择的选项更改两个不同的隐藏输入值。一个输入值需要所选选项的值。 (这是工作。)第二个隐藏的输入值需要所选选项的ID。 (这不起作用。)我已经搜索了一个解决方案,但没有找到答案。如何更改2个隐藏的输入值与选择选项值和使用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> 

回答

0

我不知道哪些值去的地方(这是由你),但你错过了什么是让所选择的选项的值,这里是代码:

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); 
    } 

希望这帮帮我。

+0

谢谢豪尔赫。这很好。 –

0

下面的变化将用于固定您的问题是有用的:

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

    } 

试用一下这个场景。我希望它有帮助

+0

谢谢Vidhya。我感谢你的回应。 –