Pomster
Pomster

Reputation: 15197

Why wont this Code execute?

This Run like it is perfectly fine.

// Save, set state to finalized and Print
        $('#btnDialogPrint').click(function () {

            if ($('#chkFinal').is(':checked')) {
                $(function () {
                    $("#PrintDialog").dialog('close');    
                });
            }
            else {
                $('#chkFinal').attr('checked', true); // Set finalized. This checkbox is not visible its like a hidden field
                $('#btnSubmit').click(); // Save
            }

        });

And this also runs fine:

window.location = '../Print/' + $('#SalesContractId').val();

But when i put them together it only run's the

window.location = '../Print/' + $('#SalesContractId').val();

Complete Code:

// Save, set state to finalized and Print
    $('#btnDialogPrint').click(function () {

        if ($('#chkFinal').is(':checked')) {

            $(function () {
                $("#PrintDialog").dialog('close');
                window.location = '../Print/' + $('#SalesContractId').val();    // Moves to ContractController Print
            });
        }
        else {
            $('#chkFinal').attr('checked', true); // Set finalized. This checkbox is not visible its like a hidden field
            $('#btnSubmit').click(); // Save
             window.location = '../Print/' + $('#SalesContractId').val();    // does not alow above code to execute 
        }

    });

Upvotes: 0

Views: 59

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

Remove the $(function() { ... }); inside the click handler and put it ouside:

$(function() {
    $('#btnDialogPrint').click(function () {
        if ($('#chkFinal').is(':checked')) {
            $("#PrintDialog").dialog('close');
            window.location.href = '../Print/' + $('#SalesContractId').val();
        } else {
            $('#chkFinal').attr('checked', true);                
            $('#btnSubmit').click();
        }
    });
});

The $(function() { ... }); means document.ready.

This being said you seem to be calling some $('#btnSubmit').click();. Note that if this #btnSubmit is actually the submit button of some form (as its id suggests) then when the form is submitted it will automatically redirect the browser to the action attribute of the form. So it is completely meaningless to call window.location.href to redirect in this case. I guess you will have to rethink whatever you are trying to achieve.

Upvotes: 4

Related Questions