jquery get and set input value of various types

get selected value

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;

The second element of the radio radio group is the currently selected value

$('input[@name=items]').get(1).checked = true;

Get the value:

Text box, text area:

$("#txt").attr("value");

$("#txt").val();

Multiple selection box checkbox:

$("#checkbox_id").attr("value");

Radio group radio:

$("input[@type=radio][@checked]").val();

Drop-down box selection:

$('#sel').val();

Control form elements:
textbox, textarea:

$("#txt").attr("value",''); // empty content 
$("#txt").attr("value",'11'); // fill content

Multiple selection box checkbox:

$("#chk1").attr("checked",''); // do not check 
$("#chk2").attr("checked", true ); // check 
if ($("# chk1").attr('checked')==undefined) // Determine whether it has been ticked

Radio group radio:

$("input[@type=radio]").attr("checked",'2' );
 // Set the item with value=2 as the currently selected item

Drop-down box selection:

$("#sel").attr("value",'-sel3'); // Set the item with value=-sel3 as the currently selected item 
$("<optionvalue='1'>1111</option><optionvalue ='2'>2222</option>" )
.appendTo( "#sel") // Add the option of the drop-down box 
$("#sel").empty(); // Empty the drop-down box

In Jquery, use $("#id") to get the input element of the page, which is equivalent to document.getElementById("element") However, the acquisition is a Jquery object, not a dom element object. The value is dom The properties of the element object. Therefore, the method of using $("#id").value to get the value is as follows:

Value:

val = $("#id")[0].value;
$("#id")[0].value = "new value";

Assignment:

$("#id")[0].value = "new value";
或者$("#id").val("new value");
val = $("#id").attr("value");

 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325286886&siteId=291194637