id属性不能有空格

1. js判断checkebox是否被选中

var ischecked = document.getElementById("xxx").checked  //返回false or true

设置选上/不选:document.getElementById("xxx").checked=true/false

Jquery

判断选上:$('#isAgeSelected').attr('checked') // true//false

选上:

$('.myCheckbox').prop('checked', true) or
$('.myCheckbox').attr('checked', true) or
$(".myCheckBox").checked(true);

不选改为false

2. jquery 判断selecte的值

<select name="selector" id="selector">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>

$('select[name=selector]').val()

选中Option 1

$('select[name=selector]').val("1");

3. 往Array中存入对象,

var regions = new Array();

var ri = new Object();
ri.id = "";
ri.name = "";
regions.push(ri);
<c:forEach items="${regionList.values}" var="region">
ri = new Object();
ri.id = "${region.id}";
ri.name = "${region.name}";
regions.push(ri);
</c:forEach>
window.regions = regions;

取数据则 Object region = regions[index];

var regionId = region.id;

Note: js中嵌入了<c:foreach>不建议这么用。

4. bind 与 unbind

$("#name").bind("click", (function () {
$("#attrib_UNIQUE_ACTV_PURCHASE").attr('checked', true);
}));
$("#description").bind("click", (function () {
$("#attrib_UNIQUE_ACTV_PURCHASE").attr('checked', false);
}));
$("#message").bind("click", (function () {
$("#name").unbind("click");
$("#description").unbind("click");
}));

增加点击后选中/不选中或解绑后点击效果

click为eventType,还有其他的

Mouse Events Keyboard Events Form Events Document/Window Events
click keypress submit load
dblclick keydown change resize
mouseenter keyup focus scroll
mouseleave   blur unload

5.判断元素是否存在:

How do I test whether an element exists?

Use the .length property of the jQuery collection returned by your selector:

1
2
3
4
5
if ( $( "#myDiv" ).length ) {
 
$( "#myDiv" ).show();
 
}

Note that it isn't always necessary to test whether an element exists. The following code will show the element if it exists, and do nothing (with no errors) if it does not:

$( "#myDiv" ).show();

or:

It seems some people are landing here, and simply want to know if an element exists (a little bit different to the original question).

That's as simple as using any of the browser's selecting method, and checking it for a truthy value (generally).

For example, if my element had an id of "find-me", I could simply use...

var elementExists = document.getElementById("find-me");
if (
elementExists != null){dosomething}

This is spec'd to either return a reference to the element or null.

Js/jquery常用的更多相关文章

  1. 【js】前端 js/jquery 常用代码和实践

    1.获取某天后几天的日期 //d为传入的日期 days为d后面的几天function getAfterDate(d,days){ var dd = new Date(d); dd.setDate(dd ...

  2. js+jquery 常用选择器函数

    一.获取当前标签 JS: this,如下: <button onclick="fun(this)"></button> Jquery,如下: $(" ...

  3. js|jquery常用代码

    页面重定位: window.location.replace("http://www.bczs.net"); window.location.href = "http:/ ...

  4. JS,JQUERY 常用笔记

    JSON.parse() 转成数组对象 JSON.stringify() 转成JSON字符串

  5. JS||JQUERY常用语法

    cookieEnabled属性语法 通常可以在浏览器的临时文件夹中保存一个文件,此文件可以包含用户信息(比如浏览过什么页面,是否选择了自动登录)等,这个文件被称作cookie,通过cookieEnab ...

  6. Js jquery常用的身份证号码 邮箱电话等验证

    刷了很多博客,https://www.cnblogs.com/hao-1234-1234/p/6636843.html 只有这个比较靠谱.

  7. js/jquery/html前端开发常用到代码片段

    1.IE条件注释 条件注释简介 IE中的条件注释(Conditional comments)对IE的版本和IE非IE有优秀的区分能力,是WEB设计中常用的hack方法.条件注释只能用于IE5以上,IE ...

  8. js与jquery常用数组方法总结

    昨天被问数组方法的时候,问到sort()方法是否会改变原来的数组.本来我猜是不会,也是这么说,马上我又觉得,知识这种东西,不确定的时候直接说不确定或不知道就好,只是凭借着不确定的猜测或者是记忆,害人害 ...

  9. js和jQuery常用选择器

    笔者觉得js是前台基础中的基础,而其选择器则是js基础中的基础,因长期使用框架导致js生疏,所有查资料,回顾一下js的常用选择器: 1.document.getElementById("id ...

随机推荐

  1. python + pytest基本使用方法(断言)

    #pytest 的基本用法# 安装: pip install pytest#在当前目录下运行 : 输入 pytest# 1.断言#功能:用于计算a与b相加的和def add(a,b): return ...

  2. glibc库和glib库

    http://ftp.acc.umu.se/pub/GNOME/sources/ ubuntu16上glib的安装包的名是libglibXX  XX是版本号 ubuntu上查看glibc的方法 ldd ...

  3. Guava - Map

    创建Map 通常在创建map时使用new HashMap<>();的方法,guava提供了一个简洁的方法 Maps.newHashMap(); List转换Map List<Solu ...

  4. GraphPad Prism 9.0安装破解教程

    graphpad prism 9.0是一款强大的科学软件,拥有大量分析图表,prism是回归分析的著名软件之一,非常适用于科研生物医学等领域.本文提供其破解版,激活码,序列号,破解教程等,可以完美激活 ...

  5. Python3中datetime时区转换介绍与踩坑

    最近的项目需要根据用户所属时区制定一些特定策略,学习.应用了若干python3的时区转换相关知识,这里整理一部分记录下来. 下面涉及的几个概念及知识点: GMT时间:Greenwich Mean Ti ...

  6. videojs文档翻译-api

    直播流地址 rtmp://live.hkstv.hk.lxdns.com/live/hks API 接口 (一)   Modules  模块 1)         browser :浏览器 2)    ...

  7. 使用js实现全选功能。(全选,全不选,反选)

    作业210721 提交代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...

  8. Python 统计列表中重复元素的个数并返回其索引值

    需求:统计列表list1中元素3的个数,并返回每个元素的索引 list1 = [3, 3, 8, 9, 2, 10, 6, 2, 8, 3, 4, 5, 5, 4, 1, 5, 9, 7, 10, 2 ...

  9. Object.defineProperty 中 get set 用法

    就是两个函数,只要搞清楚get.set的执行时机就可以了.执行时机如下代码: <!DOCTYPE html> <html lang="en"> <he ...

  10. 【LeetCode】169. 多数元素

    169. 多数元素 知识点:数组:排序:消消乐:分治: 题目描述 给定一个大小为 n 的数组,找到其中的多数元素.多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素. 你可以假设数组是非空的, ...