开发者

jQuery and IE not playing nice

I have this section of code:

function createDownload() {
        var category, format, specification, download;

        $('#submitform').click(function() { 
            category = $('#cate').val(); 
            format = $('#form').val(); 
            开发者_如何转开发specification = $('#spec').val(); 

            if (category == "NULL" || format == "NULL" || specification == "NULL") { 
                alert("Please select all options.");
                return false;
            } else {
                download = "pdfs/"+specification+format+category+".pdf"; 
                window.open(download);
            }
        });
    }

Now... In Internet Explorer it says there is an "Error on the page" - Message: 'return' statement outside of function and I have to click the button again.

In Firefox, Chrome and Safari - I have to click the button twice to get the PDF to appear... (and no errors)...

Now why could that be?!

As per request - My Form declaration: <form method="post" action="javascript: return false;" onSubmit="createDownload();">


<form method="post" action="javascript: return false;" onSubmit="createDownload();">

Since you are using return false not in a function it is throwing the error. You will have to put that in an anynymous function.

Something like

<form method="post" action="javascript: function() {return false;}" onSubmit="createDownload();">

will work

Better to bind the event .submit() for the form

<form method="post" id="frm1"></form>

$("#frm1").submit(function(){
    // your code
    return false;
});


You need to change your action='javascript: return false;' and remove the onsubmit. It should become action='javascript: createDownload();'


Try removing the return. It seems you don't even need it, and if that's really the cause of the problem then what's the point keeping it there?


First, you need to remove the action from the form - it serves no purpose here. Also, assuming #submitForm refers to the form submit button, your first click will attach the click handler and the second click will actually call it. Instead, remove the onsubmit attribute and just attach the handler in the usual way to either the button click or form submit event, being sure to cancel the event in either case:

jQuery(function($) {
    $('#submitform').click(function() {
    // or: $('#formID').submit(function() {
        var category, format, specification, download;

        category = $('#cate').val(); 
        format = $('#form').val(); 
        specification = $('#spec').val(); 

        if (category == "NULL" || format == "NULL" || specification == "NULL") { 
            alert("Please select all options.");
        } else {
            download = "pdfs/"+specification+format+category+".pdf"; 
            window.open(download);
        }

        return false;
    });
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜