接触js之初,小笔记
1.将string转换为int(或float)
var s = "12";
s+1;==>输出121
parseInt(s)+ 1; ==>输出13 ( parseFloat(s)+ 1; ==>输出13 )
2.Js根据现有值(status)控制RadioButtonList 的选中值
var rbl = document.getElementById('XXXXX');
var rbls = rbl.getElementsByTagName('input');
if (status == 1)
rbls[0].checked = true;
if (status == 0)
rbls[1].checked = true;
===>同样,判断RadioButton当前选中的值是哪个用 rbls[i].checked 判断
3.通过客户端控件组合成服务器端RadioButtonList 控件(加快页面加载速度,减少服务器端内存使用)
<input id="cb1" type="radio" onclick="get1(this);" name="cbCheck" /><label for="cb1">数值1</label>
<input id="cb2" type="radio" onclick="get2(this);" name="cbCheck" /><label for="cb1">数值2</label>
js方法:function get1(o) {o.checked = true; //其他的关联的操作代码 }
function get2(o) { o.checked = true;//..... }
4.Js计算当前所在月份的首尾日期范围
var today = new Date();
function getlastday(year, month) { //计算传入年份、月份的最后一天是几号
var lastday = new Date(year, month, 0);
return lastday.getDate();
}
function getMonthSpan() {
var startdate = today.getFullYear() + "-" + (today.getMonth() + 1) + "-" + "01";//第一天,月份值为从0-11,当前月份应显示为getMonth()的值+1
var enddate = today.getFullYear() + "-" + (today.getMonth() + 1) + "-" + getlastday(today.getFullYear(), (today.getMonth()+1));
}
5.Js控制DropDownList的下拉选项及选中项
window.onload = function () { //页面加载时对下拉选项进行控制,根据type1的选中项动态变化type2的下拉选项
var type1 = jQuery("#XXX1 option:selected").val();//读取type1的选中值
var type2 = document.getElementById("XXX2");
type2.options[0] = new Option("==请选择==");
if (type1 == 0) {
type2.options[1] = new Option("状态01", 0);
type2.options[2] = new Option("状态02", 1);
type2.options.lenght = 3;
type2.options[0].selected = true;//默认选中第一项
}
else if (type1 == 1 ) {
type2.options[1] = new Option("状态10", 2);
type2.options[0].selected = true;
type2.remove(2);
}
else {
type2.remove(1);
type2.remove(2);
}
}
6.Js控制数据项全部选中
var checkAll = function (cbx) {
var rows = document.getElementById("datalist").rows;
var row, i;
for (i = 0; row = rows[i]; i++) {
row.cells[0].getElementsByTagName("INPUT")[0].checked = cbx.checked;//第一项为checkbox
}
}