简体   繁体   中英

How do I autofill a textbox with data I just clicked with href?

在此处输入图像描述 If I click on sender 'haq', how can I redirect to the messaging page and autofill 'haq' into the username text box? 在此处输入图像描述

My code with the href is below:

return (
    <html className="u">
    <div className="form-container">
        <h1 className="message-text">Inbox</h1>
        <table className="table-box">
            <tr>
                <th>Sender </th>
                <th>Message</th>
            </tr>

            {messageList.map((val,key) => 
            {
                return (
            

            <tr>
                <a href = {"http://localhost:3000" + "/Message"}><td>{val.Sender}</td> </a>
                <td>{val.Message}</td>
            </tr>
                );
            })}
        </table>
    </div>
    </html>
);   

}

You have to get the value of the parameter message ( How to do that ). Then after getting the value, assign it to the textbox.

Example:

// Get value of parameter in URL
let example_url = "http://www.example.com/message=helloWorld"; // window.location.href
let url = new URL(example_url);
let message = url.searchParams.get("message")

// Assign value to textbox
document.getElementById("textbox").value = message;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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