gpt4 book ai didi

Javascript 类仅显示其中一个对象

转载 作者:行者123 更新时间:2023-11-30 20:52:05 25 4
gpt4 key购买 nike

我正在尝试控制台记录“选项”参数,但出于某种原因它只显示第一个对象?什么以及如何让它控制台记录“新验证器”中的所有对象?

HTML

<form>
<input type="text" id="register_username">
<input type="password" id="register_password">
</form>

Javascript

    var Validator = function (options) {
console.log(options);
};
var register_username = document.getElementById("register_username");
var register_password = document.getElementById("register_password");

document.getElementById("register_submit").addEventListener("click", function (e) {
e.preventDefault();
var register_val = new Validator(
{
element: register_username,
type: "str",
min: 1,
max: 20
},
{
element: register_password,
type: "str",
min: 4,
max: 20
}
);

}, false);

控制台日志

element: <input id="register_username" class="register_username" placeholder="Username" name="register_username" autocomplete="off" type="text">
max: 20
min: 1
type: "str"

最佳答案

您可以使用 arguments 对象访问所有传递给函数的参数。它是一个类似于对象的数组,其中包含用于调用该函数的所有参数信息。`这是一个 javascript 关键字。

来自 MDN

The arguments object is a local variable available within all (non-arrow) functions. You can refer to a function's arguments within the function by using the arguments object. This object contains an entry for each argument passed to the function, the first entry's index starting at 0.

 var Validator = function (options) {
console.log(arguments);
};
var register_username = document.getElementById("register_username");
var register_password = document.getElementById("register_password");

document.getElementById("register_submit").addEventListener("click", function (e) {
e.preventDefault();
var register_val = new Validator(
{
element: register_username,
type: "str",
min: 1,
max: 20
},
{
element: register_password,
type: "str",
min: 4,
max: 20
}
);

}, false);
<form>
<input type="text" id="register_username">
<input type="password" id="register_password">
<input type="button" id="register_submit" value="click">

</form>


关于Javascript 类仅显示其中一个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48066486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com