简体   繁体   English

从html输入中获取价值

[英]get value from html input

I am using js to fetch data from a form. 我正在使用js从表单中获取数据。 I want only to get the value from <input type="text" name="postid"> into the postid variable. 我只想将<input type="text" name="postid">postid放入postid变量中。

Here is my code. 这是我的代码。 I have added a comment where I want the value. 我在需要值的地方添加了评论。 ( var postid= ? //Here ). var postid= ? //Here )。

 $('#comment_form_sub').on('submit', function(event){ event.preventDefault(); var form_data = $(this).serialize(); //var postid= ? //Here console.log(form_data); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form class="comment-form inline-items" method="POST" id="comment_form_sub"> <input type="text" name="postid" id="postid" value="23"> <input type="text" name="comment_content" id="comment_content" class="form-control input-sm" placeholder="Press enter to post comment"> <input type="submit" style="display: none;" /> </form> 

How can I get the value from the postid input into my variable? 如何从postid输入中获取值到变量中?

There are probably many ways. 可能有很多方法。

You can do it by the following way by selecting the element directly by its name or id. 您可以通过以下方式通过选择元素的名称或ID直接选择它。

var postid = $("input[name=postid]").val() Or
var postid = $("input[id=postid]").val() Or
var postid = $('#postid').val();

If you want to get by this keyword then use any of the following ways. 如果要通过此关键字获取,请使用以下任何一种方法。

var postid = $(this).children("#postid").val() Or
var postid = $(this).children("input[name=postid]").val() Or
var postid = $(this).children("input[id=postid]").val() Or
var postid = $(this).find("#postid").val() Or
var postid = $(this).find("input[name=postid]").val() Or
var postid = $(this).find("input[id=postid]").val()

If I understand the question correctly, I think you just want to use $('#postid').val() to get the value of the postid input. 如果我正确理解了这个问题,我认为您只想使用$('#postid').val()来获取postid输入的值。 So the whole line should look like this: var postid = $('#postid').val(); 因此,整个行应如下所示: var postid = $('#postid').val();

You don't need jQuery for this. 您不需要jQuery。

Here's two ways of getting that value, one with jQuery (accepted answer) and the other with the standard methods document.getElementById and Element.addEventListener : 这是获取该值的两种方法,一种是使用jQuery(可接受的答案),另一种是使用标准方法document.getElementByIdElement.addEventListener

 // The jQuery way: $('#comment_form_sub').on('submit', function(event) { event.preventDefault(); const postId = $('#postid').val(); console.log(postId); }); // You don't need jQuery: document.getElementById('comment_form_sub').addEventListener('submit', (event) => { event.preventDefault(); const postId = document.getElementById('postid').value; console.log(postId); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form class="comment-form inline-items" method="POST" id="comment_form_sub"> <input type="text" name="postid" id="postid" value="23"> <input type="text" name="comment_content" id="comment_content" class="form-control input-sm" placeholder="Press enter to post comment"> <input type="submit" style="display: none;" /> </form> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM