我无法使jquery获取textarea的当前值。我有几个帖子,每个帖子都有“写评论”按钮。当我选择第一篇文章的按钮时,它会显示带有第一篇文章标题和textarea的对话框。当我在出现的textarea中写一些内容并按提交时,它会提醒没有。然后我点击第一个帖子上的相同按钮,它显示了我的对话框,其中包含第二篇文章的标题,当我在textarea中输入内容并按下提交时,它会提醒我输入的文字上一个对话框。你能看看我的代码并帮我找出错误。
以下是我的观点:
$data = array(
'name' => $places_id,
'class' => 'review',
'content' => 'Write a Review'
);
echo form_button($data);
<div id="write_review">
<h3><?php echo $title; ?></h3>
<textarea rows='6' cols='90' maxlength='600' id="review_text"></textarea>
</div>
这是我的JS文件:
$(document).ready(function () {
$(".review").click(function () {
var self = this;
var places_id_review = $(self).attr("name");
$("#write_review").dialog({
title: "Write a Review",
modal: true,
draggable: false,
width: 600,
height: 300,
buttons: [{
text: 'Submit',
click: function () {
var review_text = $("#review_text").val();
alert(review_text);
$.post('filter/post_review', {
places_id_review: places_id_review,
review_text: review_text
}, function (data) {
alert("ok")
}, "json");
}
}, {
text: 'Cancel',
click: function () {
$(this).dialog('close')
}
}]
});
});
});
答案 0 :(得分:1)
这是因为DOM中的对话框有重复 至少你可以得到这样的价值:
var review_text = $("#review_text", this).val(); //to prevent lookup in while DOM
但您最好控制对话框的重复项(使用&#39;销毁&#39;关闭事件&#39;关闭&#39;):
$(".review").click(function () {
var self = this;
var places_id_review = $(self).attr("name");
$("#write_review").dialog({
title: "Write a Review",
modal: true,
draggable: false,
width: 600,
height: 300,
close:function(){ $(this).dialog('destroy'); }, // DELETE dialog after close
buttons: [{
text: 'Submit',
click: function () {
$(this).dialog('close'); // do not forget close after submit
var review_text = $("#review_text").val();
alert(review_text);
$.post('filter/post_review', {
places_id_review: places_id_review,
review_text: review_text
}, function (data) {
alert("ok")
}, "json");
}
}, {
text: 'Cancel',
click: function () {
$(this).dialog('close');
}
}]
});
});