使用ajax从不同形式获取价值

时间:2017-05-13 15:38:15

标签: 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='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();

  });

例如,当我点击第二或第三种形式的提交按钮时,我总是从第一种形式获得价值。可能我需要在.MyForm或数据中使用$(this),但我不知道如何正确地做到这一点。

0 个答案:

没有答案