如何在没有服务器的情况下使用 HTML 表单
我不是网络开发人员,想了解传递变量的更好方法。过去我使用过各种方法将东西传递给java脚本函数。我从未使用过表单,因为我总是将它们与服务器和数据库相关联。我有一个网站,用户的选择会更改网站的内容。
我想知道您是否可以在没有任何服务器的情况下使用表单,将一些内容传递给 javascript 函数,在该函数中它们用于更改页面内容。对于基本示例,如果有人选择男性,则页面背景变为蓝色,如果他们选择女性,则背景变为粉红色。表单是一种可行的方法,只需 onsubmit 调用 javascript 函数吗?我实际上如何将表单内容传递给 javascript 函数?
I am not a web-developer and want to understand the better way to pass variables. In the past I have used various ways to pass things to java script functions. I have never used forms as I always associated them with server and databases. I have a website in which user selections change the contents of the website.
I am wondering if you can use forms without any server just as a way to pass a few things to a javascript function where they are used to change the page content. For basic example, if someone selects male the page background becomes blue, if they choose female the background becomes pink. Would forms be the way to go and just onsubmit call a javascript function? How would I actually pass the form contents to the javascript function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
是的,您绝对可以使用表单/输入/任何类型的 html 元素,并且永远不会与服务器通信,只是不要期望存储该数据!您使用事件(如您提到的 onsubmit 事件)来触发 Javascript 函数是正确的。
这是一个快速而肮脏的例子(肮脏的重),它确实有点像你想要的那样。请注意,我不会在颜色更改之前等待表单提交,而是在他们从下拉列表中选择性别后立即执行此操作。
http://jsfiddle.net/wG8K4/1/
Yes you absolutely can use forms/inputs/any kind of html element and never talk to a server, just don't expect to store that data! You're right about using events (like the onsubmit one you mentioned) to trigger Javascript functions.
Here is a quick and dirty example (heavy on the dirty) that does sorta kinda what you'd like. Note that instead of waiting for the form to be submitted before the color change, I go ahead and do it immediately after they choose a gender from the dropdown.
http://jsfiddle.net/wG8K4/1/
你不会传递参数。您可以让“onsubmit”调用 javascript 函数,然后在该函数中使用 javascript 访问用户已选择的实际控件。您可以使用 GetElementById 函数检索某个元素,然后确定该元素的值。
如果您只想更改背景颜色,则可以使用 javascript 更改 body 标记或页面上任何标记的 backgroundColor 属性。
不过,您必须记住从函数中返回 false - 否则,表单将被提交。
You wouldn't pass the parameters. You could have "onsubmit" call a javascript function, and then within the function use javascript to access the actual controls that the user has selected. You could use the GetElementById function to retrieve a certain element, and then determine the value of that element.
If all you wanted to do was change the background color, you could use javascript to change the backgroundColor property of the body tag or any tag on the page.
You'd have to remember to return false from your function, though -- otherwise, the form would be submitted.
您不需要服务器/数据库来使用表单。表单只是一种将变量从一个文件传递到另一个文件的方法,无论该文件是 html 文件还是某些 php 脚本或其他文件。如果您坚持使用 GET 表单,您的表单自然会将其数据打包到页面的 URL 中,此时您可以访问它们。例如(借自 http://www.onlineaspect .com/2009/06/10/reading-get-variables-with-javascript/):
You don't need servers / databases to use forms. Forms are simply a method from passing variables from one file to another, regardless if that is an html file or some php script or what have you. If you stick to using GET forms, your form will naturally pack its data into the URL of your page at which time you can access them. For instance (borrowed from http://www.onlineaspect.com/2009/06/10/reading-get-variables-with-javascript/):
您要查找的基本事件是表单的
submit
事件。如果您同意只使用事件处理程序,则可以执行以下操作:因为您只是使用 JavaScript,所以您不希望实际提交表单。 (旁白:因为您使用的是 JS,所以您应该构建此表单并将其添加到页面与 JS,但这是一个完全不同的问题。)您可以像这样取消表单的默认操作:
在我们开始访问数据之前,请确保您已获取所需的元素。它使事情变得更快一些,因为您不必在每次提交表单时都从 DOM 中选择整个元素。例如:
根据您拥有的表单元素类型,有不同的方法来访问其数据。文本输入、文本区域和选择框非常简单:
单选按钮和复选框稍微复杂一些,因为您必须仔细检查每一个并查看哪些被选中以及哪些被选中不是/不是,就像这样:
以蓝色/粉色背景为例,您可能需要类似于这样的内容:
注释
如果您确实决定使用事件处理程序,请记住一次只能将一个事件处理程序应用于您的表单。如果您还想为
submit
事件附加不同的函数,则必须使用事件侦听器,这是一个完全不同的球类游戏,并引入了以下问题:它自己的。最后一个示例仅检查“我是一个男人”元素的值,因为(大概)您正在使用单选按钮并且只有两个选项。如果“我是男人”没有被选中,那么另一个应该被选中。但是,如果表单一开始就没有选中任何选项,那么这可能会被视为错误。
最后一个示例还使用内联样式来更改正文的背景颜色。打字让我很痛苦。一种更容易忍受的方法是根据需要添加/删除类,但同样,这超出了这个问题的范围。
The basic event you're going to look for is the form's
submit
event. If you're okay with just using event handlers, you can just do something like this:Because you're just using JavaScript, you don't want the form to actually submit. (Side point: Because you're using JS, you should just build this form and add it to the page with JS, but that's a completely different issue.) You can cancel the form's default action like so:
Before we get into accessing data, make sure that you grab the elements you're going to need. It makes things a little faster, because you don't have to select the entire element from the DOM every time the form is submitted. For example:
Depending on what kind of form elements you have, there are different ways to access their data. Text inputs, text areas, and select boxes are really easy:
Radio buttons and check boxes are a little more complicated, because you have to go through every single one and see which one(s) is/are checked and which one(s) isn't/aren't, like so:
Going with your example of blue/pink background, you would probably want something similar to this:
Notes
If you do decide to go with event handlers, keep in mind that only one can be applied to your form at a time. If you want to attach a different function to the
submit
event as well, you'll have to go with event listeners, which is a completely different ball game and introduces problems of its own.The final example only checks the value of the "I'm a guy" element, because (presumably) you're using radio buttons and you only have two options. If "I'm a guy" is not checked, then the other one should be. But if the form starts out with neither option checked, then that could be considered a bug.
The final example also uses inline styles to change the body's background color. It hurt me to type. A much more bearable method would be to add/remove classes as needed, but again, that's kind of beyond the scope of this question.
表单元素可以用作全局变量的形式 - 保存可由单个页面中的所有 javascript 使用和共享的状态。
然而,这可能会导致代码脆弱且难以理解。
我建议只要您处于单个页面的上下文中,就继续将参数传递给函数。
如果您需要将数据传递到另一个页面,那么表单可以让生活变得更轻松 - 使用
GET
表单,表单上的值将传递到表单action 属性作为键值对。如果您使用
POST
方法,它们将在标头中传输。Form elements can be used as a form of global variables - holding state that can be used and shared by all the javascript in a single page.
However, this could result in brittle and difficult to understand code.
I suggest keeping with passing parameters to functions, so long as you are in the context of a single page.
If you need to pass data to another page, then forms can make life easier - using a
GET
form, the values on the form will be passed to the page referenced in the formaction
attribute as key-value pairs. If you use thePOST
method they will be transferred in the headers.如果您希望使用 Javascript 迭代表单元素,您可以使用 DOM 轻松完成此操作,因为表单将具有
length
属性,并且每个input
将表示为索引这个类似数组的对象:所以对于:
你可以这样做:
参见示例→< /a>
If your desire is to iterate through form elements using Javascript you can easily do this using the DOM since the form will have a
length
property and eachinput
will be represented as an index of this array-like object:So for:
You could do something like this:
See example →