
可以在元素创建之后获取,也可以在on事件当前动态监听元素的变化。
如果是确定的结构,可以在元素创建之后获取id的值。
如果不确定,可以使用on方法,参数加上需要监听的元素,在创建完成之后就可以自动获取了。
要获取标签个数需要用到size()函数用法,size() 方法返回被 jQuery 选择器匹配的元素的数量。 例子:
<script language="javascript" type="text/javascript">$(document)ready(function(){
var i=$('div')size();//获得div标签的数目
var j=$('ff')size();//获得class为ff的font标签的数目
alert("div有"+i+"个;font有"+j"个");
})
</script>
<div>div1</div><br>
<div>div2</div><br>
<div>div3</div><br>
<div>div4</div><br>
<font class="ff">111</font>
<font class="ff">222</font>
<font class="ff">333</font>
基本思路:遍历select下的option,使用val()获取每个option的值,然后加入到一个数组中。根据这个思路,可以使用for循环或者jQuery的each()遍历函数实现。这里提供一个更简洁的代码:
$("option")map(function(){return $(this)val();})get()join(", ")
代码说明:
使用map()函数把每个option的值传递到当前匹配集合,生成包含返回值的对象;
使用 get() 处理返回的对象以得到基础的数组;
使用join()函数组装字符串。
实例演示如下:
1、HTML结构
<select id="test">
<option value="option-1">option-1</option>
<option value="option-2">option-2</option>
<option value="option-3">option-3</option>
</select><br>
<input type="button" value="获取所有option值">
2、jquery代码
$(function(){
$(":button")click(function() {
var str = $("#test option")map(function(){return $(this)val();})get()join(", ")
alert(str);
});
});
3、效果演示
这个是jquery很详细的 你可以看看
:nth-child(index/even/odd/equation)
匹配其父元素下的第N个子或奇偶元素
':eq(index)' 只匹配一个元素,而这个将为每一个父元素匹配子元素。:nth-child从1开始的,而:eq()是从0算起的!
可以使用:
nth-child(even)
:nth-child(odd)
:nth-child(3n)
:nth-child(2)
:nth-child(3n+1)
:nth-child(3n+2)
--------------------------------------------------------------------------------
Matches the nth-child of its parent
While ':eq(index)' matches only a single element, this matches more then one: One for each parent The specified index is one-indexed, in contrast to :eq() which starst at zero
返回值
Array<Element>
参数
index (Number) : 要匹配元素的序号,从1开始
示例
在每个 ul 查找第 2 个li
HTML 代码:
<ul>
<li>John</li>
<li>Karl</li>
<li>Brandon</li>
</ul>
<ul>
<li>Glen</li>
<li>Tane</li>
<li>Ralph</li>
</ul> jQuery 代码:
$("ul li:nth-child(2)") 结果:
[ <li>Karl</li>, <li>Tane</li> ]
获取 checkbox的选中个数可以直接使用如下jquery语法
$("input[type='checkbox']:checked")length;实例演示如下
创建Html元素
<div class="box"><span>点击按钮获取选中个数:</span><br>
<div class="content">
<input type="checkbox" name="test" >萝卜
<input type="checkbox" name="test" >青菜
<input type="checkbox" name="test" >小葱
<input type="checkbox" name="test" >豆腐
<input type="checkbox" name="test" >土豆
</div>
<input type="button" class="btn" value="获取被选择个数">
</div>
设置css样式
divbox{width:300px;height:250px;padding:10px 20px;margin:20px;border:4px dashed #ccc;}divbox>span{color:#999;font-style:italic;}
divcontent{width:250px;height:100px;margin:10px 0;padding:5px 20px;border:2px solid #ff6666;}
input[type='checkbox']{margin:5px 10px;}
input[type='button']{width:120px;height:30px;margin:10px;border:2px solid #ebbcbe;}
编写jquery代码
$(function(){$("input:button")click(function() {
alert($("input[type='checkbox']:checked")length);
});
})
观察效果
使用ajax或者jquery动态创建的元素,例如'<select
id="dbc">',是无法用$("#dbc")获取的。
用法如下:
$(selector)live("event", data, function());
data为可选参数,规定传递到该函数的额外参数
eg:
$("#store")live("change", function() {
$("#username")val($("#store")val() + "_" + $("#limits")val());
});
$("#username")是使用ajax动态创建的'<select id="username">'
1.HTML结构
< input type = "checkbox" name = "test" value = "1" / > < span > 1 < / span >
< input type = "checkbox" name = "test" value = "2" / > < span > 2 < / span >
< input type = "checkbox" name = "test" value = "3" / > < span > 3 < / span >
< input type = "checkbox" name = "test" value = "4" / > < span > 4 < / span >
< input type = "checkbox" name = "test" value = "5" / > < span > 5 < / span >
2.Javascript代码:
函数show(){
Obj文件。getElementsByName(“测试”);
Check_val=[];
For(kinobj){
如果(obj[k]。检查)
Check_val。Push(obj[k]。值);
}
警报(check_val);
}
扩展资料:
JQuery对复选框的各种 *** 作:
1.根据id获取复选框:
$("#cbCheckbox1");
2.得到所有的复选框:
$("input[type='checkbox']");//或
$("input[name='cb']");
3.获得所有选中的复选框:
$("input:thecheckboxchecked");//或
$("input:[type='checkbox']:checked");//或
$("input[type='checkbox]:checked");//或
$("input:[name='ck']:checked");
以上就是关于jquery取动态id的值全部的内容,包括:jquery取动态id的值、jquery如何获得html标签数量、jQuery怎么获取Select的option个数等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)