How to get the Specific Form Field value using JQuery

I have a form

<form id="post_comment" action="cmt.php" method="post"> <input type="hidden" name="type" value="sub" /> <textarea id="body"></textarea> </form>

I am accessing the form using this code

$("#post_comment").submit(function(event){ var form = $(this); });

How can I get the value of <input type="hidden" name="type" value="sub" /> from this form. I tried to get using form.input("type") but it is not working.

Answer1:

$("#post_comment").submit(function(event){ var inputValue = $("input[name='type']",this).val(); });

Answer2:

Try using an id like this:

<form id="post_comment" action="cmt.php" method="post"> <input type="hidden" id='hidden' name="type" value="sub" /> <textarea id="body"></textarea> </form>

and later:

$("#post_comment").submit(function(event){ var hiddenValue = $("#hidden").val(); });

Answer3:

var form = $(this); var inputValue = form.find('input[name="type"]').val(); or var form = $(this); var inputValue = form.find('input:hidden').val();

Answer4:

<form id="post_comment" action="" method="post"> <input type="hidden" class="hidden" name="type" value="sub" /> <textarea id="body"></textarea> <input type="submit" value="submit" class="submit"/> </form> $(".submit").click(function(){ var hiddenVal=jQuery("#post_comment .hidden").val(); //alert(hiddenVal); });

Answer5:

$("#post_comment").submit(function(event){ var form = $("input[name='type']").val(); })

人吐槽 人点赞

Recommend

Comment

用户名: 密码:
验证码: 匿名发表

你可以使用这些语言

查看评论:How to get the Specific Form Field value using JQuery