Passing javascript variable to html textbox

2019-01-22 15:59发布

i need help on html form.

I got a javascript variable, and I am trying to pass the variable to a html form texbox. I want to display the variable on the textbox dynamically. but i do not know how to pass the variable to the html form and call the variable?

var test;

<INPUT TYPE="TEXT" NAME="lg" VALUE="" SIZE="25" MAXLENGTH="50" disabled="disabled"><BR><BR>

How do i pass test to html form and change its value?

Thanks

6条回答
姐就是有狂的资本
2楼-- · 2019-01-22 16:36

You could also use to localStorage feature of HTML5 to store your test value and then access it at any other point in your website by using the localStorage.getItem() method. To see how this works you should look at the w3schools explanation or the explanation from the Opera Developer website. Hope this helps.

查看更多
Emotional °昔
3楼-- · 2019-01-22 16:38
<form name="input" action="some.php" method="post">
 <input type="text" name="user" id="mytext">
 <input type="submit" value="Submit">
</form>

<script>
  var w = someValue;
document.getElementById("mytext").value = w;

</script>

//php on some.php page

echo $_POST['user'];
查看更多
Ridiculous、
4楼-- · 2019-01-22 16:42

Pass the variable to the form element like this

your form element

<input type="text" id="mytext">

javascript

var test = "Hello";
document.getElementById("mytext").value = test;//Now you get the js variable inside your form element
查看更多
冷血范
5楼-- · 2019-01-22 16:45

This was a problem for me, too. One reason for doing this (in my case) was that I needed to convert a client-side event (a javascript variable being modified) to a server-side variable (for that variable to be used in php). Hence populating a form with a javascript variable (eg a sessionStorage key/value) and converting it to a $_POST variable.

<form name='formName'>
<input name='inputName'>
</form>

<script>
document.formName.inputName.value=var
</script>
查看更多
疯言疯语
6楼-- · 2019-01-22 16:53

instead of

document.getElementById("txtBillingGroupName").value = groupName;

You can use

$("#txtBillingGroupName").val(groupName);

instead of groupName you can pass string value like "Group1"

查看更多
别忘想泡老子
7楼-- · 2019-01-22 16:53
document.getElementById("txtBillingGroupName").value = groupName;
查看更多
登录 后发表回答