admin管理员组

文章数量:1279175

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

e.g. the JavaScript way to acplish this:

txtShipCity.Text = txtCity.Text;

Thanks!

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

e.g. the JavaScript way to acplish this:

txtShipCity.Text = txtCity.Text;

Thanks!

Share Improve this question asked Jun 14, 2010 at 18:01 aronaron 2,88611 gold badges51 silver badges79 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 9

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 ClientIDs 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

本文标签: javascriptAspnet Set Textbox 1 to Equal Textbox 2Stack Overflow