Javascript 输入字段值未定义
Javascript Input field value undefined
我遇到的问题是我无法访问我的一个文本字段的值。我知道最初加载文档时,值将是未定义的。但是当我在单击提交时访问输入字段的值时,我必须在文本框中获取确切输入的字符串。
$("#foo").submit(function(event)
但即使在此事件侦听器之后,我也无法访问该值。
console.log(comment_value);
始终显示未定义。我也使用了 val() 函数的 jquery 版本,但没有帮助。
请指导。谢谢
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#foo").submit(function(event){
var comment_value = document.getElementById("newComment").value;
console.log(comment_value);
event.preventDefault();
// $("#newcommentblock").html('<img src="a.gif"/>');
request = $.ajax({
url: "getcomment.php",
type: "POST",
timeout:30000,
dataType: "text",
data:{getquestionid: 12, getcomment: "comment_value"}
});
alert("inside script");
request.done(function (response, textStatus, jqXHR){
console.log("Hooray, it worked!");
//$("#comments").html(response);
});
request.fail(function (jqXHR, textStatus, errorThrown){
console.error("The following error occured: " + textStatus + ", " + errorThrown);
});
request.always(function () {});
console.log("hi inside");
});
</script>
</head>
<body>
<h1>
question: <br> jisofqw<br>
</h1>
<p id = "comments">
comments <br>
</p>
Type your comment here
<div id = "newcommentblock">
<p id = "newComment"> </p>
<form id = "foo" name = "commentbox" action = "#" >
<input type = "text" id ="newComment" name = "newComment">
<input type = "submit" value = "post a comment">
</form>
</div>
<p id = "answerswer">
answer
</p>
</body>
</html>
问题就在这里
<p id="newComment"></p>
为标记提供不同的 ID <p>
。
相关文章:
- 在wordpress一定时间后更改自定义字段
- Meteor-添加用户自定义字段的方法不起作用
- 微笑时间轴小部件添加自定义字段
- 使用具有用户定义字段的新请求者创建新的Zendesk票证
- WooCommerce-根据自定义字段添加费用
- 有了字段的名称,我如何用空白数据初始化对象的未定义字段
- 高级自定义字段按单选按钮值排序
- 从自定义DocuSign按钮添加信封自定义字段
- 如何使用inlineformset_factory添加自定义字段属性
- jQuery将地理补全输出到自定义字段
- 如何在自定义帖子类型中添加自定义字段
- 根据课程自定义字段在课程页面上添加链接
- 在Extjs中,获取自定义字段容器中的textfield值
- 角度过滤器分组依据过滤器返回一个“未定义”字段
- JavaScript/JQuery:添加未定义字段中的数字(带0)
- 表单上未定义字段的$error
- Javascript -从对象中删除未定义字段
- 主干模型中的自定义字段在集合中使用时未定义
- Javascript更新并忽略目标中的未定义字段
- Jade到HTML未定义字段