Home >Web Front-end >JS Tutorial >Jquery determines whether radio, sellct, checkbox is selected and summarizes the methods of obtaining the selected value_jquery

Jquery determines whether radio, sellct, checkbox is selected and summarizes the methods of obtaining the selected value_jquery

WBOY
WBOYOriginal
2016-05-16 16:04:021344browse

jquery gets the value of radio button

Copy code The code is as follows:

$("input[name='items']:checked").val();

Another: Determine whether the radio is selected and obtain the selected value

As shown below:
Copy code The code is 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 value, checkbox value, select value, radio selection, checkbox selection, select selection, and related ones

Get the value of a group of radio selected items
Copy code The code is as follows:

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

Get the text of the selected item
Copy code The code is as follows:

var item = $("select[name=items] option[selected]").text();

The second element of the select drop-down box is the currently selected value
Copy code The code is as follows:

$('#select_id')[0].selectedIndex = 1;

The second element of the radio radio selection group is the currently selected value
Copy code The code is as follows:

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

     
Get value:

Text box, text area: $("#txt").attr("value");
Multi-select box 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",'');//Clear content

Copy code The code is as follows:

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

 
Multi-select box checkbox: $("#chk1").attr("checked",'');//Unchecked
Copy code The code is as follows:

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

Radio selection 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

Copy code The code is as follows:

$("").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’m not familiar with many things

When using $("#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");

Each in jQuery is very easy to use, and it is often used to replace the for loop of javascript

For example, if there is an each in a function, if a certain condition in each is true, the function will return true or false

Copy code The code is as follows:

function methodone(){
....
$.each(array,function(){
if(condition is established){
return true;
}
});
....
}

It turned out that it was always wrong.

After searching for information, I discovered that break and continue cannot be used in each code block. To implement the functions of break and continue, other methods must be used
break----use return false;
continue --use return ture;

So when I want to use return true in each to return this function, I actually just let each continue to execute with
Even each is not interrupted, so function cannot return

Another: Determine whether the radio is selected and get the selected value

As shown below:
Copy code The code is as follows:

function checkradio(){
var item = $(":radio:checked");
var len=item.length;
if(len>0){
alert("yes--the selected value is:" $(":radio:checked").val());
}
}
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