jquery/javascript post to new window when pressing a preview button
I want to have a form with two buttons, submit & preview, the preview button should open a开发者_JS百科 new window with the text from the input areas how do I make that happend?
<form method='post'>
//input
<input type='text' name='headline' id='headline' />
<textarea name='content' id='content'></textarea>
//buttons
<input type='submit' id='btnsubmit' value='submit' />
<input type='submit' id='btnpreview' value='preview' />
</form>
I can't have target'_blank' in form because i have two buttons, and only one should open in a new window, i would like this to be done in jquery. can someone help.
If you want to post the form to a new window, you need to set the target of the form. You can set the target for each button:
$(function(){
$('#btnsubmit').click(function(){
this.form.target = '';
});
$('#btnpreview').click(function(){
this.form.target = '_blank';
});
});
You can onclick change the target of the form:
<input type='submit' onclick="this.form.removeAttribute('target')" id='btnsubmit' value='submit' />
<input type='submit' onclick="this.form.setAttribute('target','_blank')" id='btnpreview' value='preview' />
But: the problem, I think Marcel likes to point at is:
Once you've clicked on the preview-button, the target is set to _blank. If you now do an [enter] on the <input>
, the target would'nt be reset to the default-target, it will remain _blank, what's not the desired behaviour.
You can fix this for example that way:
<form onsubmit="document.getElementById('btnsubmit').click();return false;">
//input
<input type='text' name='headline' id='headline' />
<textarea name='content' id='content'></textarea>
//buttons
<input type='submit' onclick="this.form.removeAttribute('target');this.form.submit();return false;" id='btnsubmit' value='submit' />
<input type='submit' onclick="this.form.setAttribute('target','_blank');this.form.submit();return false;" id='btnpreview' value='preview' />
</form>
精彩评论