jquery submit()忽略对输入标记的更改

时间:2014-09-30 12:44:55

标签: jquery

我有一个jquery.submit()事件处理程序。我想要做的是捕获提交,并根据某些标准,修改提交的表单上的输入标记的一些值(实际上,擦除它们空白)。当我使用调试器时,我看到字段被正确修改,但是一旦表单提交,我看到它已经提交了原始值。有没有办法做我想做的事情?

$('#views-exposed-form-find-a-rep-page-1').submit(function(event) {
    //$('.find-a-rep-search').click(function() {
        var address = $('#edit-geo-location');
        var countyField = $('#edit-field-county-value');
        //var stateField = $('#edit-state');
        var postalField = $('#edit-postal-code');
        var contact = $('#edit-field-rep-contact-name-value');
        var disabled = $('input:disabled');
        if(address.val() !== "") {
            var geocoder = new google.maps.Geocoder();
            geocoder.geocode( { 'address': address.val()}, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) { //if the input wasn't total garbage
                    var county = '';
                    var state = '';
                    var postal = '';
                    ....more logic here....                    }
                    //LOGIC:
                    //1) If we have county and state, use that to validate
                    //2) If we don't have county, use state and zip to validate
                    if (county !== '' && state !== '') {
                        countyField.val(county + ", " + state);
                        //stateField.val(state);
                        postalField.val(postal); //don't need this, but don't think it will harm
                        contact.val(""); //wipe this field so that it doesn't show up in the search url
                        disabled.prop('disabled', false); //undisable any disabled fields so views query works correctly
                        //$('#edit-name-or-location-0-wrapper input[name="name-or-location"][value="0"]').attr('checked','checked'); //reset this
                        //$('#views-exposed-form-find-a-rep-page-1').submit();
                    }
                    else if(county == "" && state !== '') {
                        var message = 'Location not specific enough, please re-enter.';
                        $('.message-box').html(message);
                        event.preventDefault();
                    }
                    else if (county == '' && state == '') { // use zip code instead
                        countyField.val(""); //wipe
                        //stateField.val(state);
                        postalField.val(postal);
                        disabled.prop('disabled', false); //undisable any disabled fields so views query works correctly
                        //@todo - do we want it to die here if the geocode doesn't have a county field?
                        //$('#views-exposed-form-find-a-rep-page-1').submit();
                    }
                }
                else {
                    var message = 'Invalid location, please re-enter.';
                    $('.message-box').html(message);
                    event.preventDefault();
                }
            });

        }
        else if(contact.val() !== "") {
            countyField.val(""); //wipe these fields
            //stateField.val("");
            postalField.val("");
            address.val("");
            disabled.prop('disabled', false); //undisable any disabled fields so views query works correctly
            //$('#views-exposed-form-find-a-rep-page-1').submit();
        }
        else {
            var message = 'No representative name or location has been provided.';
            $errorMessageBox = $('.section div.messages.error');
            if($errorMessageBox.length){
                $errorMessageBox.html(message);
            }else{
                $('#content-area').prepend('' + message + '');
            }
            event.preventDefault();
        }
    }); //end click function

1 个答案:

答案 0 :(得分:1)

您需要将单击事件附加到提交按钮并更改其中的值以及提交表单

$('#submit_button').click(function(event) {
    event.preventDefault();
   // make the changes here and afterwards submit the form
    $('#views-exposed-form-find-a-rep-page-1').submit();
});

你也可以这样尝试

$('#views-exposed-form-find-a-rep-page-1').submit(function(event) {
    event.preventDefault();
   // make the changes here and afterwards submit the form
    $('#views-exposed-form-find-a-rep-page-1').submit();
});