2

我有一个正在创建的表单,由于它的敏感性,我正在进行相当多的 javascript 验证,如果未启用 JS,我不能允许用户在表单上启动。过去,我会发布关于使用 NOSCRIPT 标签的通知。我看到它们仍然可以在 HTML5 中使用,但它们似乎即将淘汰。是否有处理这种情况的新/首选方法?

谢谢。

4

3 回答 3

2

With more HTML5 stuff, JavaScript isn't required for quite as much. For instance, form validation doesn't need JS anymore. Audio/Video doesn't require JavaScript or Flash anymore. And so on.

However, <noscript> remains the most reliable way to inform people that JavaScript is needed for certain features. The alternative is to just put the "JavaScript is needed" text in the page, and use JavaScript to remove it.

于 2012-10-22T17:47:23.333 回答
0

In the past I have done this with a redirection prior to the page.

Something like window.location() to your form and them a meta-refresh tag to a page saying it doesn't support it.

The problem is if there is no JavaScript how do you hide or prevent things on the page.

You could do what I suggested on the same page. Have a meta refresh load by default and then have the javascript stop it.

Or perhaps have javascript set a hidden variable and then you can block the form server side.

Just some options. Other than using NOSCRIPT tags but those are probably the best.

于 2012-10-22T17:47:52.693 回答
0

您应该始终在服务器端提供验证并返回任何不带 javascript 的验证错误。Javascript 验证永远不应该被当作实际的数据验证来依赖。

disabled话虽如此,您可以简单地在输入上使用属性呈现表单,而在消息中使用 no action,然后使用 javascript 将操作添加到表单并删除禁用属性。formnoscript

<form id="myform" method="post">
 <input type="text" name="something" id="myinput1" disabled />
 <input type="submit" id="sbmtbtn" disabled />
</form>
<noscript>You must have Javascript!</noscript>
<script>
  document.getElementById('myform').setAttribute('action', '/formpost/url');
  document.getElementById('myinput1').removeAttribute('disabled');
  document.getElementById('sbmtbtn').removeAttribute('disabled');
  // You get the picture  
</script>
于 2012-10-22T17:49:09.373 回答