How to get original input attributes
I use qQuery as a glue to obtain/search original DOM elements by ID to use their original properies and methods. jQuery provides the function get() for this. How does this work with the input element?
<html>
<head>
<script src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.min.js"></script>
<script>
$(function () {
var textField = $('#inputId').get();
//var textField = document.getElementById("inputId");
alert("textField = " + textField +
" name = " + textField.name +
" value = " + textField.value);
});
</script>
</head>
<body>
<form>
<input id="inputId" value="3" na开发者_运维技巧me="region"/>
</form>
</body>
</html>
The example works with document.getElementById(), but it fails with jQuery. Why? How can I get with jQuery the same object as with document.getElementById()?
Either use get(0)
or just [0]
:
var textField = $('#inputId').get(0);
or
var textField = $('#inputId')[0];
get()
without a parameter returns an array of elements, even if there is just one. So you need to specify a zero based index.
you might want to try and use:
$('#inputId').get(0)
specifying that you want the element at index 0 of the returned array of elements. Most likely the get() function is returning an array containing one element, but it's still an array.
Try $('#inputId').get(0);
精彩评论