Get value of hidden input, send to another hidden input

核能气质少年 提交于 2019-12-11 18:02:31

问题


I have a div that looks like this:

<div class="Block Moveable Panel AddToWishlistLink" id="SideProductAddToWishList" style="display:">
<div class="BlockContent">
    <form name="frmWishList" id="frmWishList" action="null" method="get">
        <input type="hidden" name="action" value="add" />
        <input type="hidden" name="product_id" value="117" />
        <input type="hidden" name="variation_id" value="" class="WishListVariationId"/>
        <input type="submit" class="wishlist-button" value="Add to wishlist" alt="Add to Wish List" />
    </form>
</div>

I need a JavaScript that can take the value of product_id, what ever it might be and input to a hidden input with the name and ID of "FormInput528" so that the value of said hidden input is changed to, in this case, 117. I am still VERY new to JS so any help would be amazing! Thank you!


回答1:


If you're using JQuery:

var product_id_val = $("#product_id").val();
$("#FormInput528").val(product_id_val);



回答2:


I think this (please note my comment) will help you.

To get the value of the hidden field:

var id = document.getElementById("product_id").value;

to implement it into another input...

var input = document.createElement("input");
input.setAttribute("type", "hidden");
input.setAttribute("value", id);
input.setAttribute("id", "FormInput528");
input.setAttribute("name", "FormInput528");

And then append your input to your form with:

document.getElementById("frmWishList").appendChild(input);



回答3:


Try:

var doc = document;
function E(e){
  return doc.getElementById(e);
}
function grabVal(name){
  return "<input type='hidden' id='FormInput528' name='FormInput528' value='"+ doc.getElementsByName(name)[0].value+"' />";
}
E('frmWishList').innerHTML += grabVal('product_id');


来源:https://stackoverflow.com/questions/18263627/get-value-of-hidden-input-send-to-another-hidden-input

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!