开发者

Why IE9 invokes get method instead of post in enter is pressed, how to fix?

开发者 https://www.devze.com 2023-04-05 22:08 出处:网络
Enter key is used to post form below. Pressing enter sends get request Iin IE9: first enter sends get, second enter sends post, third enter send get request etc.

Enter key is used to post form below. Pressing enter sends get request Iin IE9: first enter sends get, second enter sends post, third enter send get request etc.

How to send only post request if enter is pressed ? In firefox, only post request is sent properly.

<form id="Form" method='post' target='DocumentRegisterReportpdf'
    action='/erp/Report/Render?_entity=DocumentRegisterReport'>
... form fields

<select class="ui-widget-content ui-corner-all" id="_Report" name="_Report" size="10">
<option selected="selected" value="AKART001">Report1</option>
<option value="A3001">Report2</option>
<option value="A3003">Report3</option>
</select>

<input id='_submit' type='submit' value='Show PDF' />
</form>
    <script type="text/javascript">
        $(function () {
            $('#_Report').keypress(function (e) { if (enter(e)) { $('#Form')[0].submit(); cancel(e); } });
            $(开发者_JAVA百科'#_submit').focus();
        });

function enter(e) { return e.keyCode === $.ui.keyCode.ENTER && !e.ctrlKey && !e.altKey; }

function cancel(evt) {
    evt.returnValue = false;
    evt.keyCode = 0;
    evt.cancelBubble = true;
    evt.preventDefault();
    evt.stopPropagation();
}
   </script>


$('#_Report').keypress(function (e) { 
    if (enter(e))
        {
            var action = $('#Form').attr('action'),
                separator = action.indexOf('?') == -1 ? '?' : '&',
                url = action + separator + $('#Form').serialize(),
                post_or_get = 'post';
            $.ajax({
                 url : url,
                 method : post_or_get, 
                 success : function(data){
                     // this is the callback :)
                 }
            });
        }
});

or you could clone the form, change it's method from post to get and submit it :

$('#_Report').keypress(function (e) { 
    if (enter(e))
        {
             var post_or_get = 'post';
             $('#Form')
                 .clone()
                 .attr('method', post_or_get)
                 .submit();
        }
});
0

精彩评论

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