Jacob
Jacob

Reputation: 131

jquery form .submit() only works in Chrome

I'm trying to post a form that created dynamically, and see the result in a new window, and it only works in Chrome. Both FF and IE just do nothing when hitting that code.

$('<form>').attr({ method: 'POST', action: data.Url, target: '_blank' })
    .append($('<input>').attr({ type: 'hidden', name: 'Field1', value: data.field1Data }))
    .append($('<input>').attr({ type: 'hidden', name: 'Field2', value: data.field2Data }))
    .submit();

What can cause that?

Upvotes: 1

Views: 560

Answers (2)

Esailija
Esailija

Reputation: 140210

The form needs to be in the document at least in IE, probably in firefox too it seems:

$('<form>').attr({ method: 'POST', action: data.Url, target: '_blank' })
    .append($('<input>').attr({ type: 'hidden', name: 'Field1', value: data.field1Data }))
    .append($('<input>').attr({ type: 'hidden', name: 'Field2', value: data.field2Data }))
    .appendTo("body")
    .submit()
    .remove();

Upvotes: 5

derki
derki

Reputation: 620

try appending the form inside your html first, add at the end before calling submit() eg:

.appendTo('body').submit();

Upvotes: 0

Related Questions