javascript 无法在“文档”上执行“querySelector”:提供的选择器为空

问题描述 投票:0回答:1

我收到错误:

无法在“文档”上执行“querySelector”:提供的选择器为空。

当我第二次调用该方法并在使用

this.#email = document.querySelector(this.#email).value
时向前调用该方法时,但是当我使用传递给该方法的参数(如
document.querySelector(email_input_id).value
中所示)时,一切正常。我要补充的是,该功能是由点击事件触发的。

buttonClicked(email_input_id,passowrd_inpiut_id,password_repeat_input_id){
        this.clear_error_div();
        let email_input_id2 = this.#email;
        this.#email = document.querySelector(email_input_id2).value; // works first time only
        this.#password = document.querySelector(passowrd_inpiut_id).value; // works
        this.#password_repeat = document.querySelector(password_repeat_input_id).value; // works
        console.log(typeof this.#email);  // output is string
        console.log(typeof email_input_id); // output is string
        console.log(this.#email); // output is #email_inp
        console.log(email_input_id); // output is #email_inp
        //this.#email = document.querySelector(this.#email).value; // first time only then error
        // this.#password = document.querySelector(this.#password).value; // same
        // this.#password_repeat = document.querySelector(this.#password_repeat).value; // same
        this.#password_strength();
        this.#passwords_equality();
        this.#password_proper_length();
        this.#error_container.innerHTML += this.error_message;
        
        return this.#password_email_okay();
        for (var key in this.errors) {
            var value = errors[key];
            console.log(key + " = " + value + '');
        }
    }

我已经尝试记录所有内容,但我被困住了。

javascript javascript-objects queryselector
1个回答
0
投票

第一次它有效,但之后就不行了,因为我实际上已经重新分配了。固定代码如下:

 buttonClicked(){
    this.clear_error_div();
    console.log(typeof this.#email_id);
    console.log(this.#email_id);
    this.#email_value = document.querySelector(this.#email_id).value;
    this.#password_value = document.querySelector(this.#password_id).value;
    this.#password_repeat_value = document.querySelector(this.#password_repeat_id).value;
    this.#password_strength();
    this.#passwords_equality();
    this.#password_proper_length();
    this.#error_container.innerHTML += this.error_message;
    
    return this.#password_email_okay();
    for (var key in this.errors) {
        var value = errors[key];
        console.log(key + " = " + value + '');
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.