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

javascript - How to get the Specific Form Field value using JQuery - Stack Overflow

programmeradmin9浏览0评论

I have a form

<form id="post_ment" 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_ment").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.

I have a form

<form id="post_ment" 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_ment").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.

Share Improve this question asked Apr 22, 2013 at 9:46 Rashid FarooqRashid Farooq 3655 silver badges17 bronze badges 1
  • 1 try var a=$("input:hidden").val(); – Mahesh Patidar Commented Apr 22, 2013 at 9:52
Add a ment  | 

6 Answers 6

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

Try using an id like this:

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

and later:

$("#post_ment").submit(function(event){
 var hiddenValue = $("#hidden").val(); 
});
<form id="post_ment" 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_ment .hidden").val();
   //alert(hiddenVal);
 });
var form = $(this);
var inputValue =  form.find('input[name="type"]').val();

or 

var form = $(this);
var inputValue =  form.find('input:hidden').val();

Another approach for this
Consider if you have multiple forms with multiple input fields having name attribute than this code will be helpful for you :

$("#formId input[name='valueOfNameAttribute']").val()
$("#formId textarea[name='message']").val()


Hope it'll help somebody.

 $("#post_ment").submit(function(event){

        var form = $("input[name='type']").val();

    })
发布评论

评论列表(0)

  1. 暂无评论