2011-10-31 51 views
1

我使用jEditable插件与datepicker,但我需要在提交到数据库之前对日期进行验证。所以我编辑jEditable-datepicker.js到这样的事情:jQuery + jEditable - 防止提交自定义输入

submit: function (settings, original) { 
    var form = this; 
    var tid = $(original).attr('id'); 
    var input = $(original).find('input'); 
    var newDate = input.val(); 
    alert('id' + tid); 
    alert('newdate' + newDate); 
    $.ajax({ 
     async: false, 
     url: '/Stock/CompareDate', 
     type: 'GET', 
     data: { id: tid, inputDate: newDate }, 
     success: function (result) { 
      alert(result); 
      if (result < 0) { 
       alert("Expiry dare cannot be earlier than storage date"); 
       return false; 
      } 
      else { 
       alert('else'); 
       return true; 
      } 
     } 
    }); 
}, 

/* attach jquery.ui.datepicker to the input element */ 
plugin: function (settings, original) { 
    var form = this, 
     input = form.find("input"); 

    // Don't cancel inline editing onblur to allow clicking datepicker 
    settings.onblur = 'nothing'; 

    datepicker = { 
     dateFormat: 'D, dd M yy' 
    }; 

    input.datepicker(datepicker); 
} 

我得到的警报if语句,这意味着该验证方法的工作原理,但无效值仍然可以提交到数据库。我不知道为什么回报虚假力量停止提交行动..我试图解决这个问题很长一段时间,但这是我接近解决方案的时间。我只是凭着知道我可以在if语句停止提交...

难道真的需要帮助这里...谢谢....

(更新:加入HTML)

我的日期场是在一个表:

 <td id="[email protected](item.FoodID)" class="storagedatepicker"> 
       @String.Format("{0:ddd, d MMM yyyy}", item.StorageDate) 
     </td> 
     <td id="[email protected](item.FoodID)" class="expirydatepicker"> 
       @String.Format("{0:ddd, d MMM yyyy}", item.ExpiryDate) 

到jeditable的调用是这样的:

$('.expirydatepicker').editable('@(Url.Action("Edit", "Stock"))', 
    { 
     type: 'datepicker', 
     indicator: 'saving...', 
     event: 'dblclick', 
     tooltip: 'Double click to edit...', 
     submit: '<img src="../../Content/Add_in_Images/ok.png" alt="ok"/>', 
     cancel: '<img src="../../Content/Add_in_Images/cancel.png" alt="cancel"/>', 
     style: 'inherit' 

    }); 
+0

请出示HTML – Hogan

+0

嗨,我加入了HTML,无论是我的日期字段在表 – shennyL

+0

仍然不清楚在哪里提交函数被调用 – Hogan

回答

0

得到它的工作不要试图停止提交,但如果验证失败,则用默认值替换输入值。由于

0

晚的答案,但也许还是对他人有用,只是return false;onsubmit参数功能,避免其提交,例如:

$('.editable').editable('/someurl', { 

    ... // all your jeditable settings 

    onsubmit: function (settings, original) { 

     if (<conditions to not submit>) 
      return false; 

    } 
});