jQuery
Node.js & JavaScript Package: jQuery Library
Categories:
Utilities
jQuery.each()
var obj = {
"flammable": "inflammable",
"duh": "no duh"
};
$.each( obj, function( key, value ) {
console.log( key + ": " + value );
});
jQuery.find()
Search for a children
element.
$(row).find('.children_name').text('hi');
Form
Checkbox
// Get all checked checkbox
$('.myCheckBox:checked');
// Set checkbox property
$checkbox.prop('checked', true);
$checkbox.prop('checked', false);
// Toggle checkbox
$checkbox.prop('checked', !$checkbox.prop('checked'));
// is checked
$('.myCheckbox').is(':checked');
Select
Selected option text
// Selected value
$("#mySelect").val();
// Selected option text
$("#mySelect option:selected").text();
Request
Request with JSON content type & JSON payload
var post_data = {
'name': 'KJ'
};
var post_data_json = JSON.stringify(post_data);
$.ajax({
url: '/some/api',
data: post_data_json,
type: "POST",
dataType: "json",
contentType: "application/json;charset=utf-8",
success: function(return_data){
console.log(return_data);
},
error: function(xhr, ajax_options, thrown_error){
console.log(xhr.status);
console.log(thrown_error);
}
});
Reference
- javascript - Setting “checked” for a checkbox with jQuery - Stack Overflow
- :checked Selector | jQuery API Documentation
- jQuery.each() | jQuery API Documentation
- 利用ajax發出POST請求,並使用json內容格式 | JysBlog
- ajax - Jquery - How to make $.post() use contentType=application/json? - Stack Overflow
- How do I get the text value of a selected option? | jQuery Learning Center