js技巧杂谈

来源:互联网 发布:淘宝怎么换货 编辑:程序博客网 时间:2024/06/05 01:05

1.js获取checkbox的值

<input type="checkbox" id="cos" name ="cnm" value="aaa">

对应j的取值js

  var selectedCid="";//获取选中的cids  var item = $("[name= 'cnm']")  alert(item.length)    for(var i=0;i<item.length;i++){         if(item[i].checked){         selectedCid+=item[i].value+",";         }    }  alert(selectedCid)

2.js获取下拉框的值

<select class="form-vcontrol" id="detectPeriod"name="detectPeriod"><option value="1">xxx</option><option value="2">xxx</option><option value="3">xxx</option><option value="4">xxx</option></select>

对应的js

    var objTime = document.getElementById("detectPeriod");    var Timeper = objTime.options[objTime.selectedIndex].value;

3.前台向后台传值以及后台向前台传值

前端

$.post('../../neteagle/clouds/env/getExiseData.action', {type : type}, function(data, textStatus) {}, "json");

后端

String type = this.getRequest().getParameter("type");List<Map<String,String>> res = this.envService.getExiseData(type,"t_cloud_problepolicy");Map<String,String> result = new HashMap<String, String>();JSONObject json = JSONObject.fromObject(result);this.getResponse().getWriter().print(json.toString());

4.checkbox 初始化取值(数据库中)以及下拉框

$.post('../../neteagle/clouds/env/getExiseData.action', {type : type}, function(data, textStatus) {// 设置table 选中var array = (data.ip_id).split(';');var item = $("[name= 'cnm']")for (var i = 0; i < array.length; i++) {for (var j = 0; j < item.length; j++) {if (item[j].value == array[i]) {item[j].checked = true;break;}}}// 设置下拉框选中var obj = document.getElementById("detectPeriod");for (var m = 0; m < obj.length; m++) {// 下拉框的长度就是他的选项数if (obj[m].value == data.trigger) {obj[m].selected = true;}}}, "json");

5.js定位tab

js

$("div[role='tabpanel']  a[aria-controls='Information']").click();

jsp

<div role="tabpanel"><!-- Nav tabs --><ul class="nav nav-tabs" role="tablist"><li role="presentation" class="active"><a href="#Information" aria-controls="Information" role="tab" data-toggle="tab">基本信息</a></li>.........

6.获取当前点击元素的子标签

$(this).children('td').text()
获取当期点击table的第二个元素的值

$(this).children('td').eq(1).text()


7.获取父元素的id(或其他)

$(this).parent().attr('id');

。。。后续不断更新

0 0
原创粉丝点击