javascript - 从 html 输入中获取值
问题描述
我正在使用 js 从表单中获取数据。我只想从变量中获取 <input type="text" name="postid">
值postid
。
这是我的代码。我在我想要值的地方添加了一条评论。( 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>
如何从postid
输入中获取值到我的变量中?
解决方案
可能有很多方法。
您可以通过以下方式通过直接按名称或 id 选择元素来执行此操作。
var postid = $("input[name=postid]").val() Or
var postid = $("input[id=postid]").val() Or
var postid = $('#postid').val();
如果您想通过此关键字获取,请使用以下任何一种方式。
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()
推荐阅读
- spring - 如何正确使用千分尺?
- javascript - Rxjs 过滤器和映射不返回任何东西
- node.js - 解决 Hyperledger Fabric 教程中的“提案响应不成功”错误
- python - 如何使用某些条件重新组合嵌套列表?
- sql - 表格视图功能很慢
- angular - 如何在 Angular 的 http 服务中添加错误页面重定向
- javascript - 如何将 Angular 2 Frontend Express JS 变量导出到 Angular 2 组件?
- c# - 在 asp.net core 中为 TempData 对象添加扩展方法
- eclipse - 是否可以找出用 Eclipse 编辑器编写的代码是否有效并相应地禁用/启用上下文菜单按钮?
- android - Firebase Auth 在 android stuio 中使应用程序崩溃