Javascript-форум (https://javascript.ru/forum/)
-   jQuery (https://javascript.ru/forum/jquery/)
-   -   Как получить все атрибуты <input> (https://javascript.ru/forum/jquery/32221-kak-poluchit-vse-atributy-input.html)

Kotakota 08.10.2012 11:24

Как получить все атрибуты <input>
 
Привет.
Как с помощью jQuery получить массив используемых атрибутов элемента?
Например:
<input name="test" id="myid" size=50>

На выходе я хочу получить:
name = test
id = myid
size = 50.
Сейчас я обращаюсь к объекту html напрямую, с помощью element = $('#myid')[0].attributes, и перебираю все атрибуты в цикле.
Но при таком подходе у объекта есть абсолютно все атрибуты (например, loop, onclick, onfocus и т.д.), а мне нужны только те, которые реально есть и указаны!

Margarita 08.10.2012 11:44

var name = $('#myid').attr('name');
var id = $('#myid').attr('id');
var size = $('#myid').attr('size');
var params = [name, id, size];

Kotakota 08.10.2012 11:49

Margarita, да, только мне нужно, что бы это все (name,id и size) попало в params, либо в цикле, либо еще как.
Я не хочу вбивать *.attr вручную!
Для этого я и создал топик.

Kotakota 08.10.2012 12:00

Я имею в виду, есть какой либо способ получить все использующиеся атрибуты объекта.
Какой либо метод, типа getAllAtribute или похожий.

bes 08.10.2012 13:29

ну отсечь лишние наверное
<script src="http://code.jquery.com/jquery-latest.min.js"></script>

<input name="test" id="myid" size=50>
<input name="test2" id="myid2" size=100>

<script>
$(function () {
	$(':text').each(function () {
		var at = this.attributes;
		var arr = [];
		for (var key in at) {
			if (at[key].nodeName != undefined) {
				arr.push(at[key].name + ' = ' + at[key].value);
			}
		}
		alert(arr)
	});
});
</script>


UPD: хотя и так не плохо
<script src="http://code.jquery.com/jquery-latest.min.js"></script>

<input name="test" id="myid" size=50>
<input name="test2" id="myid2" size=100>

<script>
$(function () {
	$(':text').each(function () {
		var at = this.attributes;
		var len = at.length;
		var arr = [];
		for (var i = 0; i < len; i++) {
			arr.push(at[i].name + ' = ' + at[i].value);
		}
		alert(arr);
	});
});
</script>


А так ещё лучше

<script src="http://code.jquery.com/jquery-latest.min.js"></script>

<input name="test" id="myid" size=50>
<input name="test2" id="myid2" size=100>

<script>
$(function () {
	$(':text').each(function () {
		var arr = [];
		$(this.attributes).each(function () {
			arr.push(this.name + ' = ' + this.value);
		});
		alert(arr);
	});
});
</script>

Kotakota 08.10.2012 14:38

bes, Вы сумасшедший! -)
Спасибо большое!

melky 08.10.2012 17:48

Цитата:

Сообщение от bes
А так ещё лучше

map как раз для такого и присобачен, почему бы не использовать его?
<script src="http://code.jquery.com/jquery-latest.min.js"></script>

<input name="test" id="myid" size=50>
<input name="test2" id="myid2" size=100>

<script>
$(function () {
	$(':text').each(function () {
		var arr;
		arr = $.map(this.attributes, function (attribute) {
			return attribute.name + ' = ' + attribute.value;
		});
		alert(arr);
	});
});
</script>

bes 08.10.2012 18:22

Цитата:

Сообщение от melky
map как раз для такого и присобачен, почему бы не использовать его?

та конечно


Часовой пояс GMT +3, время: 00:06.