jquery获取表单数据
我试图创建一个提交表单,在单击div后显示,然后检索填写的表单数据。下面是我玩了一天的测试。变量“term”始终打印为未定义。有人能帮我吗jquery获取表单数据,jquery,forms,Jquery,Forms,我试图创建一个提交表单,在单击div后显示,然后检索填写的表单数据。下面是我玩了一天的测试。变量“term”始终打印为未定义。有人能帮我吗 $(document).ready(function () { $("#nexttable").on("click", "#addcomment", function () { document.getElementById("nexttable").innerHTML = "<form action='maindatabas
$(document).ready(function () {
$("#nexttable").on("click", "#addcomment", function () {
document.getElementById("nexttable").innerHTML = "<form action='maindatabase.php' id='commentform' method='post'><textarea name='newcomment' placeholder='Start typing your comment... (required)' rows='5' cols='50' required></textarea><br />"
+ "<span id='sbutton'><input type='button' value='Submit'></span><input type='hidden' name='inputtype' value='3'></form>";
});
});
$(document).ready(function () {
$("#nexttable").on("click", "#sbutton", function () {
var $form = $(this);
var term = $form.find("input[name='newcomment']").val();
document.getElementById("nexttable").innerHTML = term;
});
});
$(文档).ready(函数(){
$(“#下一个表”)。在(“单击”,“添加注释”,函数(){
document.getElementById(“nexttable”).innerHTML=“
”
+ "";
});
});
$(文档).ready(函数(){
$(“#下一个表”)。在(“单击”、“按钮”上,函数(){
var$form=$(此);
var term=$form.find(“输入[name='newcomment']”)val();
document.getElementById(“nexttable”).innerHTML=术语;
});
});
在名为newcomment的页面上没有输入,这是一个文本区域
$form.find( "input[name='newcomment']" ).val();
什么也找不到,应该是
$form.find( "textarea[name='newcomment']" ).val();
事件处理程序中的this
也有问题,因为它是委托事件处理程序
$(document).ready(function () {
$("#nexttable").on("click", "#sbutton", function () {
var $form = $(this); // <-- HERE, the this I do believe is in reference to the #sbutton element, not anyting to do with a form
var term = $form.find("input[name='newcomment']").val();
document.getElementById("nexttable").innerHTML = term;
});
});
$(文档).ready(函数(){
$(“#下一个表”)。在(“单击”、“按钮”上,函数(){
var$form=$(this);//谢谢您的回答。我确实错过了这个问题,但即使更正了它,打印输出仍然显示变量“term”未定义。您认为代码中还有什么问题吗?啊。我想这可能是您的处理程序中的上下文,我会在我的回答中添加一些代码。它仍然显示“未定义”。谢谢你的帮助。嗯……这很奇怪,因为我准确地复制了你的代码,并做了我推荐的更改,一切都很好。我会帮你做一件事
$(document).ready(function () {
$("#nexttable").on("click", "#sbutton", function () {
var $form = $(this).parents('form'); // <-- HERE, actually get the form element, which will be a prent of this element clicked ont
var term = $form.find("input[name='newcomment']").val();
document.getElementById("nexttable").innerHTML = term;
});
});