How do I use javascript to update the values of hi

2019-07-11 17:43发布

问题:

I have the following fields:

First Name: <input type="text" id="tFName" name="tFName" maxlength="50" />
Last Name:  <input type="text" id="tLName" name="tLName" maxlength="50" />

I want to use javaScript specifically dojo to update the value of the following hidden input fields:

<input type="hidden" name="tFName" value=""/>
<input type="hidden" name="tLName" value=""/> 

what are some ways in Javascript and Dojo to accomplish this?

回答1:

If we modify the html some (setting an ID on the hidden ones) we can:

First Name: <input type="text" id="tFName" name="tFName" maxlength="50" />
<input type="hidden" id="hiddenFName" name="tFName" value=""/>


var fName = dijit.byId("tFName");
var hFName = dijit.byId("hiddenFName");

hFName.attr("value", fName.attr("value"));


回答2:

dojo.query('#tFName').val('Joe');

See the val() docs.



回答3:

In plain Javascript, you can just set the .value property:

document.<form name>.tFName.value = <whatever>
document.<form name>.tLName.value = <whatever>


回答4:

Try this: document.getElementsByName("tFName")[0].value ="abc"; document.getElementsByName("tLName")[0].value ="def";