Bha*_*mar 974 html jquery textinput jquery-selectors
使用jQuery获取和呈现输入值的方法有哪些?
这是一个:
<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" />
Run Code Online (Sandbox Code Playgroud)
小智 1618
//Get
var bla = $('#txt_name').val();
//Set
$('#txt_name').val(bla);
Run Code Online (Sandbox Code Playgroud)
RJD*_*D22 587
您只能通过以下两种方式选择值:
// First way to get a value
value = $("#txt_name").val();
// Second way to get a value
value = $("#txt_name").attr('value');
Run Code Online (Sandbox Code Playgroud)
如果您想使用直接JavaScript来获取值,请按以下步骤操作:
document.getElementById('txt_name').value
Run Code Online (Sandbox Code Playgroud)
Art*_*r79 82
有一件重要的事情需要提及:
$("#txt_name").val();
Run Code Online (Sandbox Code Playgroud)
将返回文本字段的当前实际值,例如,如果用户在页面加载后键入了某些内容.
但:
$("#txt_name").attr('value')
Run Code Online (Sandbox Code Playgroud)
将从DOM/HTML返回值.
Nic*_*ver 40
您可以value直接获取属性,因为您知道它是一个<input>元素,但您当前的使用情况.val()已经是当前的.
对于上面的内容,只需.value直接使用DOM元素,如下所示:
$(document).ready(function(){
$("#txt_name").keyup(function(){
alert(this.value);
});
});
Run Code Online (Sandbox Code Playgroud)
Sim*_*ans 16
您必须使用各种方法来获取输入元素的当前值.
方法 - 1
如果你想使用简单的.val(),试试这个:
<input type="text" id="txt_name" />
Run Code Online (Sandbox Code Playgroud)
从输入中获取值
// 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();
Run Code Online (Sandbox Code Playgroud)
将值设置为输入
// 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");
Run Code Online (Sandbox Code Playgroud)
方法 - 2
使用.attr()获取内容.
<input type="text" id="txt_name" value="" />
Run Code Online (Sandbox Code Playgroud)
我只是在输入字段中添加一个属性.value=""attribute是携带我们在输入字段中输入的文本内容的人.
$("input").attr("value");
Run Code Online (Sandbox Code Playgroud)
方法 - 3
你可以直接在你的input元素上使用这个.
$("input").keyup(function(){
alert(this.value);
});
Run Code Online (Sandbox Code Playgroud)
SO *_*eys 14
你可以得到这样的价值:
this['inputname'].value
Run Code Online (Sandbox Code Playgroud)
Where this指的是包含输入的表单.
小智 6
要获取文本框值,可以使用jQuery val()函数.
例如,
$('input:textbox').val() - 获取文本框值.
$('input:textbox').val("new text message") - 设置文本框值.
小智 5
您只需在文本框中设置值即可。
首先,你得到像这样的值
var getValue = $('#txt_name').val();
Run Code Online (Sandbox Code Playgroud)
获得输入中设置的值后,例如
$('#txt_name').val(getValue);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2251697 次 |
| 最近记录: |