这是非常不正常的。我正在尝试解决此错误 3 天,但现在它并没有让我感到紧张。所以,我有一个按钮和一个文本框,可以将文本框中的数据提交到 PHP 页面。那里的数据存储到我的数据库中。
这是PHP文件:
if(Class_Name::Function_Name($_POST["id"], $_POST["text"])) $data["status"] = "success";
else $data["status"] = "error";
echo json_encode($data);
这是JS函数:
function editPostOffer(postId, offer_id, edited_content) {
$.post("admin/post_offer/edit_post_offer.php", {id: offer_id, text: edited_content.value}, function(data) {
if(data.status == "success") {
window.location.href = "post.php?id="+postId;
} else {
console.log("A");
}
}, 'json');
}
这是按钮+ textarea(以生成的形式):
<div class="content">
<p class="content" style="display: none;">areebaaa</p>
<textarea class="hide span12" id="edited_content" style="display: inline-block;">areebaaa</textarea>
<button type="button" onclick="editPostOffer(" 13 ",=" " "56 ",=" " "assa ");"="" style="display: inline-block;">Save Edits</button>
<button type="button" class="hide btn btn-primary btn-small cancel_edits" style="display: inline-block;">Cancel Edits</button>
</div>
但是创建 HTML 的原始 JS 在这里:
var offers = <? php echo Class_Name::Function_Name($_GET["id"]); ?> ;
for (var i = 0; i < offers.length; i++) {
var date = offers[i].Date.split(" ");
document.write('<div class="row-fluid offer">' +
'<div class="span2">' +
'<img class="profile_picture" src="' + offers[i].Picture_Path + '" />' +
'</div>' +
'<div class="span10">' +
'<div class="row-fluid">' +
'<div class="username">' +
'<p style="font-weight: bold;">' + offers[i].Name + '</p>' +
'</div>' +
'</div>' +
'<div class="row-fluid">' +
'<div class="content">' +
'<p class="content">' + offers[i].Text + '</p>' +
'<textarea class="hide span12" id="edited_content">' + offers[i].Text + '</textarea>' +
'<button type="button" class="hide btn btn-primary btn-small" id="save_edits" onclick=' + editPostOffer("13", "56", "assa") + '>Save Edits</button> ' +
'<button type="button" class="hide btn btn-primary btn-small cancel_edits">Cancel Edits</button>' +
'</div>' +
'</div>' +
'<div class="row-fluid">' +
'<div class="date">' +
'<p class="pull-right"><strong><span class="muted">Offered on: </span></strong>' + date[0] + '</p>' +
'</div>');
if (offers[i].Username == "<?php echo $_SESSION["
username "]; ?>") {
document.write('<div class="controls pull-right">' +
'<a href="" class="no_link edit_offer">Edit</a> ' +
'<a href="" class="no_link" onclick="showDeleteOfferModal(' + offers[i].Offer_ID + ');">Delete</a> | ' +
'</div>');
}
document.write('</div>' +
'</div>' +
'</div>' +
'<hr />');
}
我知道错误在这里:.. onclick="editPostOffer(" 13 ",=" " "56 ",=" " "assa ");"=""..
。
有一个 EQUALTOS 和 COMMAS 的网格,但我不明白他们为什么要来?有什么办法可以解决这个问题吗?
更新:
我现在更新了我的代码,唯一出现的错误是 JS 无法找到textarea
...
'<div class="content">' +
'<p class="content">' + offers[i].Text + '</p>' +
'<textarea class="hide span12" id="edited_content">' + offers[i].Text + '</textarea>' +
'<button type="button" class="hide btn btn-primary btn-small" id="save_edits" onclick=\'editPostOffer("<?php echo $_GET["id"]; ?>","' + offers[i].Offer_ID + '","' + $("#edited_content") + '");\'>Save Edits</button> ' +
'<button type="button" class="hide btn btn-primary btn-small cancel_edits">Cancel Edits</button>' +
'</div>' +
...
这是更新JS:
function editPostOffer(postId, offer_id, edited_content) {
console.log(postId);
console.log(offer_id);
console.log(edited_content);
$.post("admin/post_offer/edit_post_offer.php", {id: offer_id, text: edited_content.val()}, function(data) {
if(data.status == "success") {
window.location.href = "post.php?id="+postId;
} else {
console.log("A");
}
}, 'json');
}
当我将数据输出到控制台时,会出现:
13
56
Uncaught TypeError: Object [object Object] has no method 'val'
最后一行的错误意味着该对象为空,这是因为 jQuery 无法找到 textarea。