您必须使用各种方法来获取输入元素的当前值。
方法 - 1
如果你想使用简单的.val()
,试试这个:
<input type="text" id="txt_name" />
从输入中获取值
// 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();
将值设置为输入
// 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");
方法 - 2
用于.attr()
获取内容。
<input type="text" id="txt_name" value="" />
我只是在输入字段中添加一个属性。value=""
属性是携带我们在输入字段中输入的文本内容的属性。
$("input").attr("value");
方法 - 3
你可以直接在你的input
元素上使用这个。
$("input").keyup(function(){
alert(this.value);
});