简体   繁体   中英

Asp.net Set Textbox 1 to Equal Textbox 2

What's the cleanest way, like 1 line of JavaScript code, to set one text box's text property to equal another?

eg the JavaScript way to accomplish this:

txtShipCity.Text = txtCity.Text;

Thanks!

In JavaScript:

document.getElementById('txtShipCity').value = document.getElementById('txtCity').value;

Sweeten it with jQuery:

$('#txtShipCity').val($('#txtCity').val());

Although you will probably have to use the ClientID s of the two textboxes, so your JS might end up looking pretty nasty, like:

document.getElementById('<%= txtShipCity.ClientID %>').value = document.getElementById('<%= txtCity.ClientID %>').value;

Providing you have id attributes on the textboxs you could easily have a one-liner in jQuery doing the following:

$("#txtShipCity").text($("#txtCity").text()); (or $("#txtShipCity").val($("#txtCity").val()); if you are dealing with an input )

If jQuery isn't really an option then try

document.getElementById("txtShipCity").value = document.getElementById("txtCity").value

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