首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery提交不提交表格

jQuery提交不提交表格
EN

Stack Overflow用户
提问于 2015-07-28 10:01:18
回答 1查看 56关注 0票数 0

我已经写了这个脚本,但是当'No‘按在对话框上时,表单将不会提交。

这是脚本所在的页面:http://www.absoluteglazing.co.uk/quotation?item=walkon

我肯定这是很简单的事情,但我想不出来。

代码语言:javascript
复制
$( document ).ready(function() { // Document ready function

$('#quoteform').submit(function(event) {  // Submit form function

    event.preventDefault(); // Stop form from submitting

// Variable declaration
    var GetLength = $('#length').val(); // Length variable
    var GetWidth = $('#width').val(); // Width variable
    var GetLengthUnit = $('#length_unit').val(); // Length variable
    var GetWidthUnit = $('#width_unit').val(); // Width variable
    var GetNoPanes = $('input[name=nopanes]:checked').val(); // Number of panes variable
    var GetLocation = $('#thelocation').val(); // Location variable
    var GetAntislip = $('#antislip:checked').val(); // Antislip checked variable
    var GetFirerated = $('#firerated:checked').val(); // Fire Rated checked variable
    var GetSolarcontrol = $('#colarcontrol:checked').val(); // Solar Control checked variable
    var GetLengthConverted
    var GetWidthConverted
    var GetPaneLength
    var GetPaneWidth
// End of ariable declaration

if(GetLength < GetWidth) { // If length is less than width
    $('#length').val(GetWidth); // Set length to width
    $('#width').val(GetLength); // Set width to length
    var GetLengthTemp = GetWidth;
    var GetWidthTemp = GetLength;
    GetLength = GetLengthTemp;
    GetWidth = GetWidthTemp;
} // End if length is less than width

if(!$.isNumeric(GetLength) || !$.isNumeric(GetWidth)) { // Check to see if length and width are numeric

    swal({  
    title: "Error!",   
    text: "Please enter a valid length and width",   
    type: "error",   
    confirmButtonColor: "#f36e21",
    confirmButtonText: "OK" });

} else { // If length and width are valid then continue

    if(GetLengthUnit == 'cm') { // cm to mm conversion
        GetLengthConverted = GetLength * 10;
    } else if(GetLengthUnit == 'm') { // m to mm conversion
        GetLengthConverted = GetLength * 1000;
    } else if(GetLengthUnit == 'inches') { //inches to mm conversion
        GetLengthConverted = GetLength * 25.4;
    } else if(GetLengthUnit == 'feet') { // feet to mm
        GetLengthConverted = GetLength * 304.8;
    } else { // if in mm then no conversion needed
        GetLengthConverted = GetLength;
    }

    if(GetWidthUnit == 'cm') { // cm to mm conversion
        GetWidthConverted = GetWidth * 10;
    } else if(GetWidthUnit == 'm') { // m to mm conversion
        GetWidthConverted = GetWidth * 1000;
    } else if(GetWidthUnit == 'inches') { //inches to mm conversion
        GetWidthConverted = GetWidth * 25.4;
    } else if(GetWidthUnit == 'feet') { // feet to mm
        GetWidthConverted = GetWidth * 304.8;
    } else { // if in mm then no conversion needed
        GetWidthConverted = GetWidth;
    }

    if(GetNoPanes == 1) { // If one pane
        GetPaneLength = GetLengthConverted;
        GetPaneWidth = GetWidthConverted;
    } else if(GetNoPanes == 2) { // If two panes
        GetPaneLength = GetLengthConverted / 2;
        GetPaneWidth = GetWidthConverted;
    } else if(GetNoPanes == 3) { // If three panes
        GetPaneLength = GetLengthConverted / 3;
        GetPaneWidth = GetWidthConverted;
    } else if(GetNoPanes == 4) { // If four panes
        GetPaneLength = GetLengthConverted / 2;
        GetPaneWidth = GetWidthConverted / 2;
    } else { // If five or more panes
        GetPaneLength = 5000; // Use 5000 for error checking 5 panes or more
        GetPaneWidth = 5000; // Use 5000 for error checking 5 panes or more
    }

    if(GetPaneLength > 2950 || GetPaneWidth > 2400) {
        swal({  
        title: "Are you sure?",   
        text: "The size of glass panes in your quotation are quite large and will increase the cost. Would you like to split this into more panes of glass?",   
        type: "info",   
        showCancelButton: true,   
        confirmButtonColor: "#DD6B55",   
        confirmButtonText: "Yes",   
        cancelButtonText: "No",   
        closeOnConfirm: true,   
        closeOnCancel: false },
        function(isConfirm){  
        if (isConfirm) {     
            // Nothing, just close dialog
        } else {  
            alert('Just testing something is happening');   
            $('#quoteform').submit();
        } });
    }

} // End check to see if length and width are numeric

}); // End submit form function

}); // End document ready function
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-07-28 10:03:31

问题在于手动提交代码$('#quoteform').submit();,当调用该代码时,它将再次调用submit事件处理程序,这反过来将防止导致该处理程序递归调用的默认操作。

相反,您可以尝试调用dom元素的submit方法,该方法不会调用submit事件处理程序。

代码语言:javascript
复制
$('#quoteform')[0].submit();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31673076

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档