Get value of hidden input, send to another hidden

2019-09-02 09:10发布

问题:

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