Get the value in an input text box

ⅰ亾dé卋堺 提交于 2019-11-26 00:08:38

问题


What are the ways to get and render an input value using jQuery?

Here is one:

<script type=\"text/javascript\" src=\"http://code.jquery.com/jquery-1.4.3.min.js\" ></script>
<script type=\"text/javascript\">
    $(document).ready(function(){
        $(\"#txt_name\").keyup(function(){
            alert($(this).val());
        });
    })
</script>

<input type=\"text\" id=\"txt_name\"  />

回答1:


//Get
var bla = $('#txt_name').val();

//Set
$('#txt_name').val(bla);



回答2:


You can only select a value with the following two ways:

// First way to get a value
value = $("#txt_name").val(); 

// Second way to get a value
value = $("#txt_name").attr('value');

If you want to use straight JavaScript to get the value, here is how:

document.getElementById('txt_name').value 



回答3:


There is one important thing to mention:

$("#txt_name").val();

will return the current real value of a text field, for example if the user typed something there after a page load.

But:

$("#txt_name").attr('value')

will return value from DOM/HTML.




回答4:


You can get the value attribute directly since you know it's an <input> element, but your current usage of .val() is already the current one.

For the above, just use .value on the DOM element directly, like this:

$(document).ready(function(){
  $("#txt_name").keyup(function(){
    alert(this.value);
  });
});



回答5:


You have to use various ways to get current value of an input element.

METHOD - 1

If you want to use a simple .val(), try this:

<input type="text" id="txt_name" />

Get values from Input

// use to select with DOM element.
$("input").val();

// use the id to select the element.
$("#txt_name").val();

// use type="text" with input to select the element
$("input:text").val();

Set value to Input

// use to add "text content" to the DOM element.
$("input").val("text content");

// use the id to add "text content" to the element.
$("#txt_name").val("text content");

// use type="text" with input to add "text content" to the element
$("input:text").val("text content");

METHOD - 2

Use .attr() to get the content.

<input type="text" id="txt_name" value="" />

I just add one attribute to the input field. value="" attribute is the one who carry the text content that we entered in input field.

$("input").attr("value");

METHOD - 3

you can use this one directly on your input element.

$("input").keyup(function(){
    alert(this.value);
});



回答6:


I think this function is missed here in previous answers

.val( function(index, value) ) 



回答7:


You can get the value like this:

this['inputname'].value

Where this refers to the form that contains the input.




回答8:


To get the textbox value, you can use the jQuery val() function.

For example,

$('input:textbox').val() – Get textbox value.

$('input:textbox').val("new text message") – Set the textbox value.




回答9:


For those who just like me are newbies in JS and getting undefined instead of text value make sure that your id doesn't contain invalid characters.




回答10:


You can simply set the value in text box.

First, you get the value like

var getValue = $('#txt_name').val();

After getting a value set in input like

$('#txt_name').val(getValue);



回答11:


Try this. It will work for sure.

var userInput = $('#txt_name').attr('value')


来源:https://stackoverflow.com/questions/4088467/get-the-value-in-an-input-text-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!