gpt4 book ai didi

javascript - 发布后无法设置表单属性值

转载 作者:行者123 更新时间:2023-11-28 08:31:35 25 4
gpt4 key购买 nike

我有表格,它看起来像:

<form method="POST" action="create.php" data-id="0" class="postForm">
<input type="hidden" id="#formId" value="1">
<textarea class="formBodyText"></textarea>
<button type="submit">Post</button>
</form>

此表单的 ajax 调用如下:

$(document).on("submit",".postFrom",function(event){
event.preventDefault();
$.post("create.php",{'formId':$("#formId").val(),'formBodyText':$(this).find('.formBodyText').val()},function(data)
{
console.log("Return : "+data); //working fine , I mean getting expected result
$(this).attr("data-id",data); // want to set the return data as form data-id attribute value
});
});

问题在于 $(this).attr("data-id",data); ,它没有为表单设置新值。

这段代码有什么问题吗?

最佳答案

尝试,

$(document).on("submit",".postFrom",function(event){
event.preventDefault();
var cache = $(this); // get the form object here and use that inside of $.post
$.post("create.php",
{'formId':$("#formId").val(),
'formBodyText':$(this).find('.formBodyText').val()
},function(data)
{
console.log("Return : "+data); //working fine , I mean getting expected result
cache.attr("data-id",data); // want to set the return data as form data-id attribute value
});
});

关于javascript - 发布后无法设置表单属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21794382/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com