Home  >  Article  >  Web Front-end  >  Example of jQuery obtaining input, checkbox, radio, and select based on ID_jquery

Example of jQuery obtaining input, checkbox, radio, and select based on ID_jquery

WBOY
WBOYOriginal
2016-05-16 16:39:571132browse

input:

var xxx = $('#ID').val()

-------------------------------------------------- ----------------------------------

checkbox:

var xxx = [ ];
$('input[name=MyName]:checked).each(function(index, element) {
xxx.push($(element).val());
// 或者 xxx.push($(this).val());
});

Note: This is not based on ID, which is a bit loose. If your checkbox element is a child element of a container (assuming the container's id is con_id), you can choose like this:

======

$('#con_id input[name=MyName]:checked')
或者
$('input[name=MyName]:checked', '#con_id')

-------------------------------------------------- ----------------------------------

radio:

var xxx = $(''input[name=MyName]:checked”).val();

======

Note: Nothing based on ID here. To limit the specified ID, see the checkbox above

-------------------------------------------------- ----------------------------------

select:

var xxx = $("#ID option:selected").val();

======

Note two points:

1. The selected attribute of select is "seleted", not "checked".

2. Select is "single selection" by default. If it is multiple selection, I have not tried it. Presumably it can be handled with .each().

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn