Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 使用ajax从不同的表单获取值_Jquery_Ajax_Pug - Fatal编程技术网

Jquery 使用ajax从不同的表单获取值

Jquery 使用ajax从不同的表单获取值,jquery,ajax,pug,Jquery,Ajax,Pug,我想从客户端到服务器获取一个值(在我的例子中是{item.description})。这是我在pug模板中的html代码 each item, index in items form#myForm.MyForm .container #allLessons.container .lessonsWrap h3.lesson #{index+1} . Lesson | p#lessonText.lesson(style=

我想从客户端到服务器获取一个值(在我的例子中是
{item.description}
)。这是我在pug模板中的html代码

each item, index in items
  form#myForm.MyForm
    .container
      #allLessons.container
        .lessonsWrap
          h3.lesson #{index+1} . Lesson |
          p#lessonText.lesson(style='margin-left:1%;' type="desc" name='desc') #{item.description}
          a(href=)
            button#butPam.btn.btn-primary(type='submit') Begin
          .about
            p(style='margin-left:1%;') Lecturer: #{item.author}
            p(style='margin-left:1%;') Level: #{item.level}
下面是ajax脚本:

script.
  $('.MyForm').on('submit', function(event){
    var createVar = $("#lessonText").html();
    var dataToSend = createVar;

    $.ajax({
      url: '/qVal',
      method: 'post',
      data: {'desc':createVar},
      async: true,
      success: function(response){
        console.log(response);
      }
    })
    event.preventDefault();

  });

例如,当我在第二个或第三个表单中单击submit按钮时,我总是从第一个表单中获取值。可能我需要在.MyForm或数据中使用$(this),但我不知道如何正确使用。

我假设它是唯一的,这样您就可以使用class来代替
#pamokaText
元素,将其更改为class后,您就可以使用
(“.pamokaText”)
但是,虽然
#pamokaText
元素在代码中不清楚,但我无法知道
表单
#pamokaText
元素之间的关系。。如果它是一个表单子项,那么您可以使用
$(this).find(“.pamokaText”).html()
My bad,它应该是一个#lessontext相同的方式。。改为class
p.lessonText
而不是ids
p#lessonText
并使用
$(这个)。查找(“.lessonText”).html()
,感谢Mohamed Yousef,这很有帮助!