I have a situation where I am using a custom jQuery plugin to hijack a form submit. The data in the form is then sent via a jsonp cross domain ajax call to another server. This data cannot ever be posted to the sever hosting the form.
My problem is that if the client has javascript disabled, my plugin won't prevent the form from submitting. How do I ensure that the form doesn't ever submit even without javascript enabled?
One option I have considered is to use javascript to write out the form tags.
<script type="text/javascript">
document.write('<form id="myForm">');
</script>
<!-- form html here -->
<script type="text/javascript">
document.write('</form>');
</s开发者_Go百科cript>
and then apply the plugin to the form
$('#myForm').myplugin();
While this would work, it seems kind of hacky, considering third party clients are going to be using my plugin and I'd rather not dictate their forms have to be written this way. Another possibility if I had to go that way is to force them to attach my plugin to a div containing the form and then auto-inject the form tags around the div and continue as normal. Something tells me, however, there has to be an easier way.
if you're not ever going to do a standard form submit, then eliminate the <form>
tags altogether replacing them with <DIV>
tags. You only need to tie the submit to any valid element, such as a span. and do a $('#myDiv').myPlugin();
Maybe I'm completely missing what you are trying to accomplish. But couldn't you simply NOT use: input type="submit" value="Submit Form" /
Instead use: input type="button" action="myValidate();" value="Submit Form" /
From what it sounds you are not wanting anybody w/o Javascript to be able to submit the form. So I would think this would be an easy solution.
<form onSubmit="return false">
Not sure if this will work if javascript is disabled...
My solution would be... not to break if Javascript is disabled. Put a script on your server that handles the cases when the form is submitted. Then have that script make requests to the foreign server and send the appropriate response back to the user. This would probably include a hint that Javascript would enhance their enjoyment of your website.
精彩评论