【问题标题】:Get value of hidden input, send to another hidden input获取隐藏输入的值,发送到另一个隐藏输入
【发布时间】:2013-08-18 06:55:06
【问题描述】:

我有一个如下所示的 div:

<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>

我需要一个 JavaScript,它可以获取 product_id 的值,无论它可能是什么,并输入到名称和 ID 为“FormInput528”的隐藏输入中,以便在这种情况下将所述隐藏输入的值更改为, 117. 我对 JS 还是很陌生,所以任何帮助都会很棒!谢谢!

【问题讨论】:

  • 使用这样的隐藏输入是一种不好的做法。我可以轻松下载 HTML 并输入我自己的值。
  • 你应该在这方面做一些工作并发布一些代码,这样人们就可以看看有什么问题,而不是为你从头开始编写代码。

标签: javascript html forms input hidden-field


【解决方案1】:

我认为这(请注意我的评论)会对您有所帮助。

获取隐藏字段的值:

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

将其实现到另一个输入中...

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

然后将您的输入附加到您的表单中:

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

【讨论】:

  • 我会在第一行代码中重命名“var id”,因为这不是一个 id - 不好的做法。
【解决方案2】:

如果您使用的是 JQuery:

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

【讨论】:

  • 你没有将这个新元素附加到任何表单中,这样就不会起作用。
【解决方案3】:

试试:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-13
    • 2015-06-30
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 2013-11-17
    相关资源
    最近更新 更多