提交一个设置了action属性的表单,同时有一个javascript代码来处理提交
当您提交一个设置了 action 属性的表单,同时有一个 javascript 代码来处理提交时,会发生什么?
对于用户没有安装 javascript 的情况,有什么方法可以维护这两个代码(操作属性和 javascript 代码)?
问候
哈维
what happens when you submit a form that has the action attribute set and in the same time there is a javascript code that handles the submitting?
Any way to maintain both codes (action attribute and javascript code) for the case the user doesn't have javascript installed?
Regards
Javi
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于“处理提交的 JavaScript 代码”的含义。假设您的意思是“您调用表单对象的
submit()
方法”,那么浏览器将正常将表单提交到指定的 URI。使用渐进增强。
编写一个无需 JavaScript 即可工作的表单,然后用执行替代功能的 JS 事件处理程序包装它(如果您不希望这样做,则可以取消默认提交)。
That depends on what you mean by "javascript code that handles the submitting". Assuming you mean "You call the
submit()
method of the form object", then the browser will submit the form as normal to the specified URI.Use progressive enhancement.
Write a form that works without JavaScript, then wrap it with JS event handlers that do alternative functions (and cancel default submission if you don't want that).
当 JavaScript 被禁用时,你没有太多的控制权。但是,您可以使用
标签< /a> 暗示应该为页面的功能启用 javascript。请注意,您还可以使用 javascript 更改
action
属性,如下所示:但是,如果禁用 javascript,这当然不起作用。
您在开发页面时应牢记优雅降级,这意味着当某些功能不存在时,您的页面至少应该仍然正常运行。如何编码页面取决于您。在这种情况下,我希望始终有一个提交按钮,以便无论是否启用 JavaScript 都会提交表单。
There is not much control you have when javascript is disabled. You can however, use
<noscript></noscript>
tags to imply that javascript should be enabled for the functionality of the page. Note that you can also change theaction
attribute with javascript like this:But of course that won't work if javascript is disabled.
You should develop your page keeping in mind graceful degradation which means when certain feature isn't there, your page should still behave normally at least. It is upto you how you code your page. I would prefer a submit button in such case to be there always so that a form is submitted whether or not javascript is enabled.