阻止在Ajax中提交表单

时间:2015-07-02 13:21:55

标签: php html ajax form-submit

我有一个带

的表格
function validate(){
 $.ajax({
        dataType: 'json',
        url: app.url.prefix,
        method: 'POST',
        data: {service: 'manage', met: 1, name: name },
        success: function (data) {
            if (data.exists){
                 return false;
            }

        }

    });
return true;
}

在我的javascript文件中我有

Ajax

data.exists代码检查返回的数据是否具有值,尤其是if (data.exists){ return false; }。我想根据存在的值来阻止提交表单。


org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:62) at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:456) at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:461) at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:461) at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:461) at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:162) at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:271) at excelreader.readExcelFile(excelreader.java:28) at excelreader.main(excelreader.java:18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:497) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140) Caused by: java.lang.reflect.InvocationTargetException at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60) ... 13 more Caused by: java.lang.OutOfMemoryError: Java heap space at org.apache.xmlbeans.impl.store.Cur.createElementXobj(Cur.java:260) at org.apache.xmlbeans.impl.store.Cur$CurLoadContext.startElement(Cur.java:2995) at org.apache.xmlbeans.impl.store.Locale$SaxHandler.startElement(Locale.java:3207) at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.reportStartTag(Piccolo.java:1082) at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseAttributesNS(PiccoloLexer.java:1822) at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseOpenTagNS(PiccoloLexer.java:1521) at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.parseTagNS(PiccoloLexer.java:1362) at org.apache.xmlbeans.impl.piccolo.xml.PiccoloLexer.yylex(PiccoloLexer.java:4678) at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.yylex(Piccolo.java:1290) at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.yyparse(Piccolo.java:1400) at org.apache.xmlbeans.impl.piccolo.xml.Piccolo.parse(Piccolo.java:714) at org.apache.xmlbeans.impl.store.Locale$SaxLoader.load(Locale.java:3454) at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1276) at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1263) at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345) at org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotCacheRecords$Factory.parse(Unknown Source) at org.apache.poi.xssf.usermodel.XSSFPivotCacheRecords.readFrom(XSSFPivotCacheRecords.java:62) at org.apache.poi.xssf.usermodel.XSSFPivotCacheRecords.<init>(XSSFPivotCacheRecords.java:53) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at org.apache.poi.xssf.usermodel.XSSFFactory.createDocumentPart(XSSFFactory.java:60) at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:456) at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:461) at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:461) at org.apache.poi.POIXMLDocumentPart.read(POIXMLDocumentPart.java:461) at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:162) at org.apache.poi.xssf.usermodel.XSSFWorkbook.<init>(XSSFWorkbook.java:271) at excelreader.readExcelFile(excelreader.java:28) at excelreader.main(excelreader.java:18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

不起作用。

3 个答案:

答案 0 :(得分:0)

$('#myform').submit(function() {
    return false;
});

这应该可以解决问题,现在表格不会在按下回车键或按钮时重新加载页面。

编辑:
您的表单也缺少双引号

<form id="myform" onsubmit="return validate();">
    <input type="text" id="name" name="name">
</form>

答案 1 :(得分:0)

您的问题是由于异步ajax函数调用而发生的,它在ajax数据返回之前返回true。 我还没检查过,但你可以尝试这样的事情:

function validate(){
var self = this;
self.preventDefault();
 $.ajax({
        dataType: 'json',
        url: app.url.prefix,
        method: 'POST',
        data: {service: 'manage', met: 1, name: name },
        success: function (data) {
            if (!data.exists){
                 self.submit();
            }

        }

    });

返回false;     }

答案 2 :(得分:0)

解决方案:

我更改了HTML,我添加的onclick事件比onsubmit

<form id="myform">
   <input type="text" id="name" name="name">
   <button type="submit" id="button" onclick="validate();">
</form>

同样在Javascript

我阻止提交

 $("#button").on("click",function(event){
    event.preventDefault();
});

这是我的功能,检查是否存在数据,然后提交表单

function validate(){

$.ajax({

 dataType: 'json',
 url: app.url.prefix,
 method: 'POST',
 data: {service: 'manage', met: 1, name: name },
 success: function (data) {
 if (!data.exists){
   $('#myform').submit();
}
 }
  });