jQuery - Make input value a var
I want to put a var text = "hello"
Into an input value. I am not sure how to do this, and I would like to know to this, as I am not sure.
开发者_开发技巧I thought It would of just been $(input#text).text(text);
but that did not work
If you look up the documentation of jQuery for text
, you'll see that text()
manipulates text nodes (or text node content). If you're setting a value on a textbox you should be using the val()
function.
$("input#text")
.val(text);
Use val()
not text()
:
$("#text").val(text);
The val()
is used when you want to read from or write to text fields.
To assign value of a text box whose id is textbox in jQuery ,do the following :-
$("#textbox").val(text);
See val
you can use
var text = "hello"
$('#id').val(text);
//where id is the id assigned to your text box
//eg.
<input type='text' id='mytextbox' value=''/>
//it should be then
var text = "hello"
$('#mytextbox').val(text);
精彩评论