HTML form being submitted twice via submit 'click' trigger

泄露秘密 提交于 2019-12-12 13:24:32

问题


I am sorry for posting a question which is similar to almost 100 questions on SO. I read a lot of them, tried a couple of solutions but was not able to either fix my problem or completely relate the answers to my issue.

  1. I have a form with an action, type post.
  2. I have a <input type="submit"> in that form.
  3. I need to submit this form by triggering a 'click' on this input. Which I do by $('#imageUploadSubmit').click();

I am not supposed to bypass 'clicking' this submit button because there are other events associated with this, which are essential and come from Struts jQuery. Hence I cannot submit the form directly.

Problem : my form action (uploadImage) is being called twice. And I am unable to prevent it.

Relevant piece of JSP:

<s:form id="image-upload-form" action="uploadImage" method="post" enctype="multipart/form-data">
    <s:file name="userImage" label="User Image" id="userImageFile" />
    <input type="hidden" id="idForUpload" name="idForUpload" value="<s:property value="imageUploadReqAttrib.imageName" />" />
    <sj:submit id="imageUploadSubmit" value="Select Image" targets="previewImageDiv" 
        indicator="indicator" onBeforeTopics="before" onCompleteTopics="complete" style="display:none;"/>
</s:form>

and the generated Html (with relevant elements)

<form id="image-upload-form" name="image-upload-form" action="/foo/uploadImage.action" method="post" enctype="multipart/form-data">
      <input type="file" name="userImage" value="" id="userImageFile"></td>
      <input type="hidden" id="idForUpload" name="idForUpload" value="1371711900.jpg">
      <input type="submit" id="imageUploadSubmit" value="Select Image" style="display:none;">
</form>

Is there a way by which I could find out all the events that are being triggered on and after that click? In Chrome Inspector? Fiddler?

I would appreciate any pointers. Thank You.


回答1:


After googling more, this is what seems to be the problem : All versions > 2.1.0 of struts2-jquery-plugin have this issue. Now, I'm not sure if the problem is the way which I am using the plugin or if it is actually a bug. An open bug in the community makes me feel that this is a bug.

So, I moved from struts2-jquery-3.3 to struts2-jquery-2.1.0 :|



来源:https://stackoverflow.com/questions/11744773/html-form-being-submitted-twice-via-submit-click-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!