可编辑的欧芹用两次调用ajax

时间:2016-01-15 11:37:53

标签: javascript jquery ajax x-editable parsley.js

我有这段代码:

$.fn.editable.defaults.mode = 'inline';
var editable = $('.x-editable').editable({
    send: 'always',
    validate: function() {
        var form = editable.next().find('form');
        if (!form.parsley().isValid()) {
            return true;
        }
    }
});
editable.on('shown', function () {
   var form = editable.next().find('form');
   form.parsley();
});

和html:

<a class="x-editable toggle-content editable editable-pre-wrapped editable-click" href="#" data-toggle-limit="110" data-toggle-limit-padding="10" data-custom-params="{&quot;inLaunchId&quot;:1489,&quot;inActivitySummaryId&quot;:71962900}" data-disabled="false" data-emptytext="Add a comment" data-escape="true" data-name="inComment" data-placeholder="" data-tpl="<textarea maxLength=&quot;4000&quot; data-parsley-pattern=&quot;^\d+$&quot;  data-popover=&quot;<p>Max length: <b>4000</b></p>&quot;></textarea>" data-type="textarea" data-url="submitURL" data-value="text">text</a>

并且由于不知道原因ajax请求被发送两次,有谁知道为什么?如何解决这个问题?这是 jsFiddle

1 个答案:

答案 0 :(得分:1)

由于您使用editable作为表单,而parsley仅用于验证 - 快速修复将禁用parsley提交表单。在这种情况下 - 字段将由bootstrap editable提交。

$.fn.editable.defaults.mode = 'inline';
var editable = $('.x-editable').editable({
    send: 'always',
    validate: function() {
    var form = editable.next().find('form');
    if (!form.parsley().isValid()) {
        return true;
    }
  }
});
editable.on('shown', function () {
  var form = editable.next().find('form');
  form.parsley().on('form:submit', function() {  
    return false; // Don't submit form 
  });;
});