Home >Web Front-end >JS Tutorial >jQuery removes spaces at the beginning and end of a string (implemented in multiple ways)_jquery

jQuery removes spaces at the beginning and end of a string (implemented in multiple ways)_jquery

WBOY
WBOYOriginal
2016-05-16 17:38:461018browse

Remove spaces from the beginning and end of the string.
jQuery code:

Copy code The code is as follows:

$.trim(" hello , how are you? ");

jquery loop to read checkbox value
Copy code The code is as follows:
Copy code The code is as follows:

$("input[type=checkbox][checked]").each(function(){ //Since the checkbox is generally selected Multiple, so you can loop and output
alert($(this).val());
});

$("#A").val("1" ) The value of id A is 1. This is the case in jQuery. When assigning, it is passed to the function as a parameter, which is different from simple js.
Like $("#A").html("1")$( "#A").text("1") is all about assignment $("#A").html() $("#A").text() is all about taking value, taking html,
taking text Text
Method 1:
Copy code The code is as follows:

$('#id ').css('display','none');
$('#id').css('display','block');

Method 2:
Copy code The code is as follows:

$('#id').hide();
$('#id').show();

radio button group, name="sex".
Copy code The code is as follows:

Male
< input type="radio" name="sex" value="Female">Female
Unknown

1. Get the value selected by the radio.
Copy code The code is as follows:

$('input:radio[name=sex]:checked').val();

2. Select the radio button (Male).
Copy code The code is as follows:

$('input:radio[name=sex]:nth(0)') .attr('checked',true);

or
Copy code The code is as follows:

$('input:radio[name=sex]')[0].checked = true;

3. Select the radio button (Female).
Copy code The code is as follows:

$('input:radio[name=sex]:nth(1 )').attr('checked',true);
or
$('input:radio[name=sex]')[1].checked = true;

4. Select the radio button (Unknown).
Copy code The code is as follows:

$(' input:radio[name=sex]:nth(2)').attr('checked',true);
or
$('input:radio[name=sex]')[2].checked = true;

5. Reset radio button.
Copy code The code is as follows:

$('input:radio[name=sex]').attr('checked',false);
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