A summary of the various ways jQuery operates on the input value

  • 2020-03-29 23:55:54
  • OfStack

Gets the selected value
Gets the values of a set of selected items of radio


var item = $('input[@name=items][@checked]').val();

Gets the text of the selected item in a select

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();

Multi-checkbox:

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

Radio group:

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

Drop-down box select:

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

Control form elements:
Text box, text area:

$("#txt").attr("value",'');//Empty content
$("#txt").attr("value",'11');//Fill the content

Multi-checkbox:

$("#chk1").attr("checked",'');//Don't tick
$("#chk2").attr("checked",true);// tick 
if($("#chk1").attr('checked')==undefined) //Determine if you have checked the box

Radio group:

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

Drop-down box select:

$("#sel").attr("value",'-sel3');//The item with value=-sel3 is the currently selected item
$("<optionvalue='1'>1111</option><optionvalue='2'> 2222</option>").appendTo("#sel")//Add options to the drop-down box
$("#sel").empty() ; //Clear the drop-down box

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

In Jquery, $("#id") is used to get the input element of the page, which is equivalent to document.getelementbyid ("element"). $(" # id ") [0]. Value = "new value"; $("#id")[0]. Value = "new value";
Or $(" # id "). Val (" new value "); Val = $(" # id "). The attr (" value ");

= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Jquery input text radio check select


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> Headless document </title>
<script src="jquery-starterkit/lib/jquery-1.3.2.min.js" type="text/javascript"></script>
</head>
<body>
<input type="text" id="dd" name="dd" value="dds"/>dd
<input name="rr" id="rr" type="radio" value="34" />ff
<input name="rr" id="rr2" type="radio" value="4" />55
<input name="ff" type="checkbox" value="aa" />jgdg
<input name="ff" type="checkbox" value="gd" />jgdg
<select name="ss" id="ss" size="1">
<option value=""></option>
<option value="8">d</option>
<option value="2">g</option>
</select>
<br/>
<input type="button" id="button" value=" Button a " />
<input type="button" id="jj" value=" Button 2 " />
<br/>
<div id="ssd">fgfooHello</div>
</body>
<script language="javascript" type="text/javascript">
<!--
$(function(){
$("#button").click(function(){

//Get the value
//alert( $('#dd').val());//type=text
// alert($('input[name=rr][checked]').val());//type=radio
// alert($('input[name=ff][checked]').val());//type=checkbox
// alert($("select[name=ss] option[selected]").val());//select  Equivalent to alert($("#ss option[selected]").val());
//Get the text
//alert($("select[name=ss] option[selected]").text());//select

// control 
/*//Disable # dd is disabled
$("#dd").attr("disabled","disabled");
//The Enable # dd
$("#jj").removeAttr("disabled");*/

// $('input[name=rr]').get(0).checked = true;// The first one radio To be selected 
//alert($("input[type=radio][value=34]").attr("checked",'checked'));//value=34 the radio To be selected 
//alert($("input[type=checkbox][value=gd]").attr("checked",'checked'));//value=gd the checkbox To be selected 
//$('input[name=ff]').get(1).checked = true;// The first one check To be selected 
 
/*//Select option according to option's text
count=$("#ss").find("option").length;
for(var i=0;i<count;i++) 
{           
if($("#ss").get(0).options[i].text == 'd') 
{ 
$("#ss").get(0).options[i].selected = true; 
break; 
} 
} */
//$("<option value='1'>1111</option><option value='25'>22s22</option>").appendTo("#ss");// increase option
//$("#ss option[value=8]").remove("");// remove  <option value='8'>d</option>
//$("#ss").attr("value",'2');// The selected option                            
//$('#ss')[0].selectedIndex = 1;// The selected option
//$("#ss").empty();// Clear all option
/*//Replace text
var $thirdLink = $("#ssd");
var linkText = $thirdLink.text().replace('foo','bar');
$thirdLink.text(linkText); */
});       
});
-->
</script>
</html> 


Related articles: