Get the value in an input text box

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"  />

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

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

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 

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.

链接地址: http://www.djcxy.com/p/26508.html

上一篇: autocomplete =“off”是否与所有现代浏览器兼容?

下一篇: 在输入文本框中获取值