如何使用Jquery设置隐藏输入字段的值?

问题描述:

我正在尝试将隐藏的输入字段的值设置为与单击的链接的值相同.

I am attempting to set the value of a hidden input field to the same as the value of a clicked link.

这是我尝试过的无效的方法:

This is what I have attempted which doesnt work:

$(document).ready(function(){
$(".delete_link").click(function(){
        var deleteID = $(this).attr("data-value"); 
        $("#delete_value").attr("value") = deleteID;
    });
});

正确设置了变量deleteID.

以及参考表格:

<form name="delete_form" action="delete_post.php" method="POST">
    <p>Please confirm you want to delete this post.</p>
    <input type="submit" id="delete_submit" name="delete_submit" value="confirm" />
    <input type="hidden" id="delete_value" value="" />
</form>

使用val方法

$("#delete_value").val(deleteID);

对于数据属性,您可能还想使用数据方法

also for data attributes you might want to use the data method

$(this).data('value');

链接:

  • http://api.jquery.com/val/
  • http://api.jquery.com/data/