Jquery determines whether radio selelct and checkbox are selected and gets the selected value method summary
- 2020-05-30 19:22:49
- OfStack
jquery takes the value of the radio radio button
$("input[name='items']:checked").val();
Also: determine whether radio is selected and get the selected value
As follows:
function checkradio(){
var item = $(":radio:checked");
var len=item.length;
if(len>0){
alert("yes-- The selected value is: "+$(":radio:checked").val());
}
}
jquery radio, checkbox, select, radio selected, checkbox selected, select selected, and so on
Gets the value of 1 set of radio selected items
var item = $('input[name=items][checked]').val();
Gets the text of the select selected item
var item = $("select[name=items] option[selected]").text();
The second element in 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");
checkbox: $("#checkbox_id").attr("value");
Radio group radio: $("input[type=radio][checked]").val();
select: $('#sel').val();
Control form elements:
Text box, text area: $("#txt").attr("value", ""); // empty the content
$("#txt").attr("value",'11');// Fill the content
checkbox: $("#chk1").attr("checked", "); / / no box
$("#chk2").attr("checked",true);// tick
if($("#chk1").attr('checked')==undefined) // Determine if you have checked the box
Radio group radio: $("input[type=radio]").attr("checked",'2'); // set value=2 for the currently selected item
Drop-down box select: $(" # sel "). attr (" value, "' - sel3 '); // set value= -sel3 as the currently selected item
$("<option value='1'>1111</option><option value='2'>2222</option>").appendTo("#sel")// Add a drop-down box option
$("#sel").empty() ; // Clear the drop-down box
At the beginning of jquery, many things are unfamiliar
While using $("#id") to get the input element of the page, $("#id").value did not get the value
Finally, with the help of the great baidu, the cause of the problem was found:
$("") is an jquery object, not an dom element object
value is a property of dom element
jquery corresponds to val
val() : gets the current value of the first matched element.
val(val): sets the value of each matched element.
So, the code should read:
Value: val = $("#id")[0].value;
Assignment: $("#id")[0].value = "new value";
Or $(" # id "). val (" new value ");
val = $("#id").attr("value");
each in jQuery is very useful and is often used to replace for loops in javascript
For example, if there is one each in one function, then if something is true in each, then return this function to true or false
function methodone(){
....
$.each(array,function(){
if( Conditions established ){
return true;
}
});
....
}
It always turned out to be wrong.
It turned out that break and continue could not be used in the each code block, but other methods were needed to achieve the functions of break and continue
break -- return false;
continue -- return ture;
So when I want to use return true in each to return to this function, I'm just going to let each continue
Even each has not been interrupted, so function cannot return
Also: determine if radio is selected and get the selected value
As follows:
function checkradio(){
var item = $(":radio:checked");
var len=item.length;
if(len>0){
alert("yes-- The selected value is: "+$(":radio:checked").val());
}
}