最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I set the value of a hidden input field using Jquery? - Stack Overflow

programmeradmin1浏览0评论

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

The variable deleteID is correctly set.

and the form for reference:

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

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

The variable deleteID is correctly set.

and the form for reference:

<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>
Share Improve this question asked Apr 26, 2013 at 16:22 crmephamcrmepham 4,76019 gold badges87 silver badges163 bronze badges 3
  • Use jQuery's data API for retrieving data-* attributes, don't use attr('data-*') – Madbreaks Commented Apr 26, 2013 at 16:24
  • where is link button which have class name .delete_link ? – rahularyansharma Commented Apr 26, 2013 at 16:24
  • Careful with the submit action. Even if the click trigger is raised, the form will be submitted and there are no warranty that your hidden field will have the value set up. You should handle the submit event of the form and not the delete click. – gustavodidomenico Commented Apr 26, 2013 at 16:30
Add a ment  | 

3 Answers 3

Reset to default 3

use the val method

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

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

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

links:

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

For all form elements you need to use .val():

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

A plete example you (http://jsfiddle/79HEY/):

HTML

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

JavaScript

$(document).ready(function(){
    $("#deleteform").submit(function(){
        var deleteID = $("#delete_submit").data("id");
        $("#delete_value").val(deleteID);
        return true;
    });
});
发布评论

评论列表(0)

  1. 暂无评论