简体   繁体   English

如何在 onclick 属性中返回异步函数的值?

[英]How to return an async function's value in onclick attribute?

This function returns a Promise.此 function 返回 Promise。

async function validateStudent(event, reg, pass) {
    if (reg != '' || pass != '') {
        var res = await requestStudent(reg, pass);
        if (res == null) {
            document.getElementById("error").innerText = "Invalid username or password.";
            return false;
        }
        else {
            document.getElementById("HiddenField1").value = res;
            return true;
        }
    }
    else {
        document.getElementById("error").innerText = "Please fill in the form.";
        return false;
    }
}

If I want to use this as an event handler, how will I get its true value without using another async function in the onclick attribute?如果我想将其用作事件处理程序,如何在不使用onclick属性中的另一个async function 的情况下获得其真实值? I want to use this function like this:我想像这样使用这个 function:

<button onclick="return validateStudent('abc', '123')">Send</button>

I tried using return Promise.resolve(validateStudent('abc', '123')) , it did not work.我尝试使用return Promise.resolve(validateStudent('abc', '123')) ,它不起作用。

The issue here appears to be that you are trying to get the button's inline onclick handler to respect and await a promise, which doesn't seem possible at this point in time.这里的问题似乎是您试图让按钮的内联 onclick 处理程序尊重并等待 promise,这在此时似乎是不可能的。

As a result, you will have to alter your approach slightly to be able to achieve your goal.因此,您将不得不稍微改变您的方法才能实现您的目标。 Instead of focusing on making this work by using the default button behavior, you can instead programmatically trigger the form submission when you get your valid promise response.您可以在获得有效的 promise 响应时以编程方式触发表单提交,而不是专注于使用默认按钮行为来完成这项工作。

Change your handler code to return void as you are no longer trying to modify the default button behavior:更改您的处理程序代码以返回 void,因为您不再尝试修改默认按钮行为:

async function validateStudent(event, reg, pass) {
    if (reg != '' || pass != '') {
        var res = await requestStudent(reg, pass);
        if (res == null) {
            document.getElementById("error").innerText = "Invalid username or password.";
        }
        else {
            document.getElementById("HiddenField1").value = res;
            document.getElementsByTagName("form")[0].submit(); // Add this line to submit form after hidden input is filled
        }
    }
    else {
        document.getElementById("error").innerText = "Please fill in the form.";
    }
}

And finally change your button to the following:最后将您的按钮更改为以下内容:

<button type="button" onclick="validateStudent('abc', '123')">Send</button>

Note that I added type="button" to avoid the default form submission behavior (if you omit the button type it becomes type="submit" by default when in a form context).请注意,我添加了type="button"以避免默认的表单提交行为(如果您省略按钮类型,则在表单上下文中默认变为type="submit" )。 I also removed the return as the button's onclick no longer cares about the return value.我还删除了return ,因为按钮的 onclick 不再关心返回值。

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

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