Home >Web Front-end >JS Tutorial >jquery code to get input form value_jquery

jquery code to get input form value_jquery

WBOY
WBOYOriginal
2016-05-16 18:29:131226browse
jquery gets the value of the radio radio button
$("input[name='items']:checked").val();
jquery radio gets the value, checkbox gets the value, select Get value, radio selected, checkbox selected, select selected, and related
Get the value of a set of radio selected items
var item = $('input[name=items][checked ]').val();
Get the text of the selected item
var item = $("select[name=items] option[selected]").text();
The second element of the select drop-down box is the currently selected value
$('#select_id')[0].selectedIndex = 1;
radio radio selection group The second element is the currently selected value
$('input[name=items]').get(1).checked = true;

Get the value:

Text box, text area: $("#txt").attr("value");
Multiple selection box checkbox: $("#checkbox_id").attr("value");
Radio selection group radio: $("input[type=radio][checked]").val();
drop-down box select: $('#sel').val();

control Form element:
Text box, text area: $("#txt").attr("value",'');//Clear content
$("#txt").attr( "value",'11');//Fill content

Multiple selection box checkbox: $("#chk1").attr("checked",'');//Uncheck
$("#chk2").attr("checked",true);//Check
if($("#chk1").attr('checked')==undefined) //Determine whether it has been checked Check

radio group radio: $("input[type=radio]").attr("checked",'2');//Set the item with value=2 as the currently selected item
Drop-down box select: $("#sel").attr("value",'-sel3');//Set the item with value=-sel3 as the currently selected item
$(" ").appendTo("#sel")//Add the option of the drop-down box
$("#sel").empty();//Clear the drop-down box

I just started to get in touch with jquery and I am not familiar with many things
When I used $("#id") to get the input element of the page, I found that $("#id").value cannot get the value

Later, with the help of the great Baidu, I finally found the cause of the problem:
$("") is a jquery object, not a dom element

value is an attribute of dom element

Jquery corresponds to val

val(): Get the current value of the first matching element.

val(val): Set the value of each matching element.

So, the code should be written like this:

Value: val = $("#id")[0].value;
Assignment:
$("#id ")[0].value = "new value";
or $("#id").val("new value");

Or this also works: val = $("# id").attr("value");
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