简体   繁体   English

客户端 javascript 文件设置表单标签属性

[英]client-side javascript file set form tag attribute

I want to make a login, signup webpage with nodejs.我想用nodejs做一个登录、注册网页。 So I use form tag to send url and method.所以我使用表单标签发送 url 和方法。 In my case form tag has two different submit buttons.在我的例子中,表单标签有两个不同的提交按钮。 below is HTML code下面是 HTML 代码

 <form id="login">
        <input type="text" placeholder="ID" id="id" name="id"> <br>
        <input type="password" placeholder="password" id="passwd" name="passwd"> <br>
        <input type="submit" value="login" id="login-btn"> 
        <input type="submit" value="signup" id="signup-btn">
    </form>
    <script src="index.js"></script>

Since I want to make these two buttons work differently I write simple js file to set forms' action and method.由于我想让这两个按钮以不同的方式工作,我编写了简单的 js 文件来设置表单的操作和方法。

const response = document.getElementById('login');
const attribute = function(obj, action, method){
    response.setAttribute('action', action);
    response.setAttribute('method', method);
}

const login = document.getElementById('login-btn');
login.addEventListener('click', attribute(response, '/login', 'POST'));

const signup = document.getElementById('signup-btn');
signup.addEventListener('click', attribute(response, '/signup', 'GET'));

When I write like that both buttons work as signup button.当我这样写时,两个按钮都用作注册按钮。 So I only can access signup page.所以我只能访问注册页面。

However, when I write js file without declaring function但是,当我在没有声明 function 的情况下编写 js 文件时

const response = document.getElementById('login');
const login = document.getElementById('login-btn');
login.addEventListener('click', () => {
    response.setAttribute('action', "/login");
    response.setAttribute('method', "POST");
});

const signup = document.getElementById('signup-btn');
signup.addEventListener('click', () => {
    response.setAttribute('action', "/signup");
    response.setAttribute('method', 'GET');
});

it works well.它运作良好。 Can you guys tell me what is different between two js codes?你们能告诉我两个js代码有什么不同吗? And why is this happening?为什么会这样?

As per the MDN documentation for [addEventListener] 1 , the syntax is given by根据[addEventListener] 1的 MDN 文档,语法由下式给出

target.addEventListener(type, listener [, options]);

listener听众

The object that receives a notification (an object that implements the Event interface) when an event of the specified type occurs.当指定类型的事件发生时接收通知的object(实现Event接口的object)。 This must be an object implementing the EventListener interface, or a JavaScript function .这必须是实现 EventListener 接口的 object 或 JavaScript function See The event listener callback for details on the callback itself.有关回调本身的详细信息,请参阅事件侦听器回调。

If you observe the 2nd argument in the following line of code, you will notice, that it's not a function , it's rather the return value of the function , which is, undefined by default (because you haven't specified any return values):如果您观察以下代码行中的第二个参数,您会注意到,它不是function ,而是function的返回值,默认情况下undefined (因为您没有指定任何返回值):

login.addEventListener('click', attribute(response, '/login', 'POST'));

To validate what I am saying here, please do the following:要验证我在这里所说的话,请执行以下操作:

typeof attribute(response, '/login', 'POST');
> undefined

So, essentially, this following line:所以,基本上,下面这行:

login.addEventListener('click', attribute(response, '/login', 'POST'));

evaluates to评估为

login.addEventListener('click', undefined);

whereas, in the following snippet, the 2nd argument is clearly an arrow function:而在下面的代码片段中,第二个参数显然是一个箭头 function:

document.getElementById('login-btn');
login.addEventListener('click', () => {
    response.setAttribute('action', "/login");
    response.setAttribute('method', "POST");
});

.. and that is the source of the difference in behavior between two. ..这就是两者行为差异的根源。

I hope I clarified your doubt.我希望我澄清了你的疑问。 If I din't, please post a comment.如果我没有,请发表评论。

You're trying to use attribute as the callback, but you're calling the function which returns undefined and when the click event happens it just tries to call undefined .您正在尝试使用attribute作为回调,但您正在调用 function ,它返回undefined并且当点击事件发生时它只是尝试调用undefined You need to wrap those attribute calls in another function to be able to run it that way.您需要将这些attribute调用包装在另一个 function 中才能以这种方式运行它。 Something like...就像是...

const login = document.getElementById('login-btn');
login.addEventListener('click', () => {
  attribute(response, '/login', 'POST')
});

const signup = document.getElementById('signup-btn');
signup.addEventListener('click', () => {
  attribute(response, '/signup', 'GET')
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM