Submit form with attach option, working in all browsers, but not IE
I recently have been having a problem with a form, on an asp site.
Basically, the form requires the user to fill it in, and then they have the option to either upload a file, or submit straight away.
Clicking on the 'attach' button the session is saved, and then they are redirected to an upload file page, once uploaded the user then clicks to return to the form, with all their details still contained; Then they can submit.
开发者_StackOverflow中文版Previously, the form was only working in Chrome and Safari. Through Stackoverflow, I found the following code which works in all broswers except IE.
$('#myForm').get(0).setAttribute('action', 'session_save.asp');
I expect that it's the stAttribute tag, but I am not sure as to what I should use instead, and searching hasn't produced me with any ideas.
My code for the submit are is as follows:
<input type="image" id="attach" value="Attach" name="action" src="images/btn_attach.gif" style="margin: 0 0 6px 0;"><br>
<input type="submit" id="send_data" value="Send" title="Submit CSR" name="action">
</form>
<!--JQUERY TO CHANGE FORM ACTION-->
<script>
$(document).ready(function(){
$("#attach").click(function(){
$('#myForm').get(0).setAttribute('action', 'session_save.asp');
<!--$('#myForm').attr({action: "session_save.asp"});-->
$('#myForm').submit();
});
});
</script>
<!--END JQUERY-->
Any help would be greatly appreciated.
It doesn't make much sense that
$('#myForm').get(0).setAttribute('action', 'session_save.asp');
works and not
$('#myForm').attr({action: "session_save.asp"});
You should use the latter, unless there is a bug in jQuery. Which version are you using? Can you give a link to your original question? I'd like to look at it.
However you shouldn't use name="action"
on your inputs, because that can "hide" the action
property of the form, which is most likely your real problem.
EDIT:
I missed that you attached the event to an input type="image"
. They submit forms on their own, unless you prevent it, which you don't. You need to have your event handler return false
(or use jQuery's event.preventDefault()
):
$("#attach").click(function(){
$('#myForm').attr({action: "session_save.asp"});
$('#myForm').submit();
return false;
});
BTW, you are using HTML comments in JavaScript. You have to use /* ... */
instead of <!-- ... -->
.
精彩评论