Web2py:如何在LOADed组件中取消表单提交

时间:2016-11-22 22:42:28

标签: ajax web2py html-form

如果我有一个web2py视图(例如tmp.load),我可以通过从提交处理程序返回false来强制不提交表单。

<form onsubmit="return false;"><input type="text"><input type="submit" /></form>

直接查看它时有效:此表单永远不会提交。但是,如果我使用

加载此视图
{{=LOAD('tmp.load', ajax=True)}}

然后表格提交,即使我没想到它。那么如何编写一个包含表格的LOAD组件,如果满足某些js条件,该表格可以编程为不提交?

1 个答案:

答案 0 :(得分:1)

web2py.js会自动为组件中的表单设置事件处理程序,以便通过Ajax提交它们。一种可能的方法是通过设置tmp.load事件处理程序来拦截web2py的Ajax提交。例如,在组件的视图中(即<script> $(document).on('ajax:beforeSend', function(e, xhr, settings) { if (settings.type === 'POST') { if(abortAjaxSubmission) { xhr.abort(); $.web2py.enableFormElements($('form#myform')); } } }); </script> ),类似于:

abortAjaxSubmission

在上文中,form#myform表示您要停止提交的条件。选择器 $scope.json = [ {"keycolumn1":1,"originkey1":1,"datafield1":1}, {"keycolumn1":2,"originkey1":2,"datafield1":2}, {"keycolumn1":3,"originkey1":3,"datafield1":3}, {"keycolumn1":4,"originkey1":4,"datafield1":4}, {"keycolumn1":5,"originkey1":5,"datafield1":5}, {"keycolumn1":11,"originkey1":11,"datafield1":11}, {"keycolumn1":12,"originkey1":12,"datafield1":12}, {"keycolumn1":13,"originkey1":13,"datafield1":13}, {"keycolumn1":14,"originkey1":14,"datafield1":14}, {"keycolumn1":15,"originkey1":15,"datafield1":15}]; 表示组件中的表单(您需要重新启用表单元素,因为web2py在提交时会禁用它们,但只有在完成Ajax请求时才重新启用它们,如果你中止了。)