[英]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 = "answer">
answer
</p>
</body>
</html>
问题在这里
<p id="newComment"></p>
为<p>
标签提供一个不同的ID 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.