webix UI 模式中的表单数据

Form data in webix UI modal

我正在使用 Webix UI 模式,我是这样使用它的:

this.add = function () {
scrollArea.css("overflow", "hidden");
$.ajax({
 type: "GET",
 url: "/detail/create",
 success: function (form) {
  webix.message.keyboard = false;
  webix.modalbox({
   title: "New detail",
   buttons: ["Accept", "Decline"],
   text: form,
   width: 400,
   callback: function (result) {
    switch (result) {
     case "0":
      addDetail();
      break;
     case "1":
      break;
    }
    scrollArea.css("overflow", "auto");
   }
  });
 }
});
function addDetail() {
 $.ajaxSetup({
  headers: {
   'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
 });
 $.ajax({
  type: "POST",
  url: "/detail/store",
  data: $('#detail_add').serialize(),
  contentType: "JSON",
  processData: false,
  success: function () {
  }
 });
}
};


And form's HTML:
<form action="" id="detail_add" method="post">
<input type="text" name="name" placeholder="Name">
<input type="text" name="article" placeholder="Article">
<input type="hidden" name="location_id" placeholder="1">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
</form>

当我在模式中单击接受时,我的 JSON 是空的。我该如何解决? 我试图通过 console.log 获取输入的值,但它也是空的。

这不是一般的答案,但示例代码不适用于解决某些问题,因为:

  • 我们不知道什么是 scrollArea 对象
  • 您尝试实现依赖于我们没有的成功脚本响应的代码
  • 我们没有带有启动代码操作的按钮

这里的代码略有改动,可以正常工作并演示您的案例:

我正在使用 Webix UI 模式,我是这样使用它的:

scrollArea = $(window.document);
this.add = function() {

  //scrollArea.css("overflow", "hidden");

  $.ajax({
    type: "GET",
    url: "/detail/create",
    beforeSend: function(form) {
      webix.message.keyboard = false;
      webix.modalbox({
        title: "New detail",
        buttons: ["Accept", "Decline"],
        text: form,
        width: 400,
        callback: function(result) {
          switch (result) {
            case "0":
              addDetail();
              break;
            case "1":
              break;
          }
          scrollArea.css("overflow", "auto");
        }
      });
    }
  });

  function addDetail() {
    $.ajaxSetup({
      headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
      }
    });

    $.ajax({
      type: "POST",
      url: "/detail/store",
      data: $('#detail_add').serialize(),
      contentType: "JSON",
      processData: false,
      success: function() {}
    });
  }
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<link rel="stylesheet" href="http://cdn.webix.com/edge/webix.css" type="text/css">
<script src="http://cdn.webix.com/edge/webix.js" type="text/javascript"></script>

<form action="" id="detail_add" method="post">
  <input type="text" name="name" placeholder="Name">
  <input type="text" name="article" placeholder="Article">
  <input type="hidden" name="location_id" placeholder="1">
  <input type="hidden" name="_token" value="{{ csrf_token() }}" />
  <button onClick="add()">Add</button>
</form>

当我在模式中单击接受时,我的 JSON 是空的。我该如何解决? 我试图通过 console.log 获取输入的值,但它也是空的。

我已经找到了解决办法。 这真的很简单,但我做错了。 我需要使用

switch (result) {
    case "0":
       addDetail;
       break;
    case "1":
       break;
}

而不是

switch (result) {
    case "0":
       addDetail();
       break;
    case "1":
       break;
}

因为 addDetail() 立即调用函数,因此我发送的数据是空的