使用JavaScript和AJAX,我是否还需要HTML 标签,是否仍需要使用“提交”按钮向服务器提交表单?

时间:2022-11-23 19:39:33

Back when I learned HTML, CGI was king, and the proper way to interact with the server was to put <input> tags inside of a <form> tag. When the user pressed the "submit" button, either an HTTP GET or an HTTP POST message was sent to the server and the browser refreshed using the HTML code returned from the CGI program on the server.

当我学习HTML时,CGI是王道,与服务器交互的正确方法是将标签放在

标签内。当用户按下“提交”按钮时,将HTTP GET或HTTP POST消息发送到服务器,并使用从服务器上的CGI程序返回的HTML代码刷新浏览器。

Now I've started doing web stuff again and I've been learning JavaScript and jQuery. Since jQuery can intercept events like click, keypress, and focusout, and can read data with $.ajax() and $.getJSON(), is there still a need to have a <form> tag at all?

现在我又开始做网页了,我一直在学习JavaScript和jQuery。由于jQuery可以拦截click,keypress和focusout等事件,并且可以使用$ .ajax()和$ .getJSON()读取数据,是否还需要一个

标签?

Maybe "real forms" are still necessary if I want to support users that have JavaScript turned off?

如果我想支持关闭JavaScript的用户,可能还需要“真实表单”吗?

3 个解决方案

#1


4  

Forms can still help you, but the short answer is no, not really.

表格仍然可以帮助你,但简短的答案是否定的,不是真的。

If you have a group of inputs that are going to pertain to a certain AJAX request, then it might be easier to keep them in their own form for validation/serialization purposes. For example:

如果您有一组与某个AJAX请求相关的输入,则可能更容易将它们保存在自己的形式中以进行验证/序列化。例如:

$.ajax({
    url: 'ajax/test.html',
    dataType: 'json',
    method: 'POST',
    content: $('form#login_form').serialize(), // <-- Serialize elements only in
    success: function(data) {                  //     the login_form container.
        $('.result').html(data);
    }
});

Depending on your requirements, you may need to keep your forms around for nice degradation for users who have Javascript turned off. You're still going to need to submit the forms, so having the form set up and in place is a good idea.

根据您的要求,您可能需要保留表单,以便关闭Javascript的用户可以轻松降级。您仍然需要提交表单,因此设置和安装表单是个好主意。

#2


1  

If you're using ajax, as long as you don't have any direct posts or gets, a form tag is not required. However, it is still good html design.

如果您使用的是ajax,只要您没有任何直接帖子或获取,就不需要表单标签。但是,它仍然是很好的HTML设计。

#3


1  

You can do both, submit or not. It can depend on which form element you want to listen to for changes. But you can do something like this as just one example:

您可以同时提交,提交或不提交。它可以取决于您想要监听哪些表单元素进行更改。但是你可以做一些像这样的例子:

<SELECT NAME="tree" onchange="doSomething( someVar );">

But again, it depends on which element of the page you want to manipulate.

但同样,它取决于您想要操作的页面的哪个元素。

#1


4  

Forms can still help you, but the short answer is no, not really.

表格仍然可以帮助你,但简短的答案是否定的,不是真的。

If you have a group of inputs that are going to pertain to a certain AJAX request, then it might be easier to keep them in their own form for validation/serialization purposes. For example:

如果您有一组与某个AJAX请求相关的输入,则可能更容易将它们保存在自己的形式中以进行验证/序列化。例如:

$.ajax({
    url: 'ajax/test.html',
    dataType: 'json',
    method: 'POST',
    content: $('form#login_form').serialize(), // <-- Serialize elements only in
    success: function(data) {                  //     the login_form container.
        $('.result').html(data);
    }
});

Depending on your requirements, you may need to keep your forms around for nice degradation for users who have Javascript turned off. You're still going to need to submit the forms, so having the form set up and in place is a good idea.

根据您的要求,您可能需要保留表单,以便关闭Javascript的用户可以轻松降级。您仍然需要提交表单,因此设置和安装表单是个好主意。

#2


1  

If you're using ajax, as long as you don't have any direct posts or gets, a form tag is not required. However, it is still good html design.

如果您使用的是ajax,只要您没有任何直接帖子或获取,就不需要表单标签。但是,它仍然是很好的HTML设计。

#3


1  

You can do both, submit or not. It can depend on which form element you want to listen to for changes. But you can do something like this as just one example:

您可以同时提交,提交或不提交。它可以取决于您想要监听哪些表单元素进行更改。但是你可以做一些像这样的例子:

<SELECT NAME="tree" onchange="doSomething( someVar );">

But again, it depends on which element of the page you want to manipulate.

但同样,它取决于您想要操作的页面的哪个元素。