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
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
$("").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
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
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