本文目录一览:
- 1、javascript 选择单选框
- 2、js如何创建那个日期选择框啊
- 3、js怎么设置select的样式,边框和三角的颜色
- 4、如何通过js实现勾选复选框
- 5、js使用DOM设置单选按钮、复选框及下拉菜单的方法
javascript 选择单选框
我只能说,你这逻辑明显不对,你应该是要为每一个radio控件绑定onclick事件,点击的时候alert一下。
你的代码有几个错误。
1、getElementsByName("radios"); 返回的是一个数组,你不能为一个数组对象绑定onclick事件
2、循环绑定js的onclick事件,需要借用闭包或者函数的方式,否则绑定的onclick事件都会是最后一个对象的方法。
以下是我用闭包的方式解决你的绑定方法,你可以看一下,把你的所有js去掉,页面不变,js换成如下代码:
script type="text/javascript"
var box = document.getElementsByName("radios");
// 循环为每个 radio 绑定 onclick 事件
for (var i = 0; i box.length; i++) {
(function() {
var boxone = box[i]
box[i].onclick = function() {
// 如果当前的 radio 被选中,则 alert 他的 value
if (boxone.checked) {
alert(boxone.value);
}
}
})();
}
/script
js如何创建那个日期选择框啊
给你推荐一个间断兼容性比较好JS代码!
html
head
titleJs日期选择器并自动加入到输入框中/title
meta http-equiv="content-Type" content="text/html;charset=gb2312"
script type="text/javascript"
var gMonths=new Array("一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月");
var WeekDay=new Array("日","一","二","三","四","五","六");
var strToday="今天";
var strYear="年";
var strMonth="月";
var strDay="日";
var splitChar="-";
var startYear=2000;
var endYear=2050;
var dayTdHeight=12;
var dayTdTextSize=12;
var gcNotCurMonth="#E0E0E0";
var gcRestDay="#FF0000";
var gcWorkDay="#444444";
var gcMouseOver="#79D0FF";
var gcMouseOut="#F4F4F4";
var gcToday="#444444";
var gcTodayMouseOver="#6699FF";
var gcTodayMouseOut="#79D0FF";
var gdCtrl=new Object();
var goSelectTag=new Array();
var gdCurDate=new Date();
var giYear=gdCurDate.getFullYear();
var giMonth=gdCurDate.getMonth()+1;
var giDay=gdCurDate.getDate();
function $(){var elements=new Array();for(var i=0;iarguments.length;i++) {var element=arguments[i];if(typeof(arguments[i])=='string'){element=document.getElementById(arguments[i]);}if(arguments.length==1){return element;}elements.Push(element);}return elements;}
Array.prototype.Push=function(){var startLength=this.length;for(var i=0;iarguments.length;i++){this[startLength+i]=arguments[i];}return this.length;}
String.prototype.HexToDec=function(){return parseInt(this,16);}
String.prototype.cleanBlank=function(){return this.isEmpty()?"":this.replace(/\s/g,"");}
function checkColor(){var color_tmp=(arguments[0]+"").replace(/\s/g,"").toUpperCase();var model_tmp1=arguments[1].toUpperCase();var model_tmp2="rgb("+arguments[1].substring(1,3).HexToDec()+","+arguments[1].substring(1,3).HexToDec()+","+arguments[1].substring(5).HexToDec()+")";model_tmp2=model_tmp2.toUpperCase();if(color_tmp==model_tmp1 ||color_tmp==model_tmp2){return true;}return false;}
function $V(){return $(arguments[0]).value;}
function fPopCalendar(evt,popCtrl,dateCtrl){evt.cancelBubble=true;gdCtrl=dateCtrl;fSetYearMon(giYear,giMonth);var point=fGetXY(popCtrl);with($("calendardiv").style){left=point.x+"px";top=(point.y+popCtrl.offsetHeight+1)+"px";visibility='visible';zindex='99';position='absolute';}$("calendardiv").focus();}
function fSetDate(iYear,iMonth,iDay){var iMonthNew=new String(iMonth);var iDayNew=new String(iDay);if(iMonthNew.length2){iMonthNew="0"+iMonthNew;}if(iDayNew.length2){iDayNew="0"+iDayNew;}gdCtrl.value=iYear+splitChar+iMonthNew+splitChar+iDayNew;fHideCalendar();}
function fHideCalendar(){$("calendardiv").style.visibility="hidden";for(var i=0;igoSelectTag.length;i++){goSelectTag[i].style.visibility="visible";}goSelectTag.length=0;}
function fSetSelected(){var iOffset=0;var iYear=parseInt($("tbSelYear").value);var iMonth=parseInt($("tbSelMonth").value);var aCell=$("cellText"+arguments[0]);aCell.bgColor=gcMouseOut;with(aCell){var iDay=parseInt(innerHTML);if(checkColor(style.color,gcNotCurMonth)){iOffset=(innerHTML10)?-1:1;}iMonth+=iOffset;if(iMonth1){iYear--;iMonth=12;}else if(iMonth12){iYear++;iMonth=1;}}fSetDate(iYear,iMonth,iDay);}
function Point(iX,iY){this.x=iX;this.y=iY;}
function fBuildCal(iYear,iMonth){var aMonth=new Array();for(var i=1;i7;i++){aMonth[i]=new Array(i);}var dCalDate=new Date(iYear,iMonth-1,1);var iDayOfFirst=dCalDate.getDay();var iDaysInMonth=new Date(iYear,iMonth,0).getDate();var iOffsetLast=new Date(iYear,iMonth-1,0).getDate()-iDayOfFirst+1;var iDate=1;var iNext=1;for(var d=0;d7;d++){aMonth[1][d]=(diDayOfFirst)?(iOffsetLast+d)*(-1):iDate++;}for(var w=2;w7;w++){for(var d=0;d7;d++){aMonth[w][d]=(iDate=iDaysInMonth)?iDate++:(iNext++)*(-1);}}return aMonth;}
function fDrawCal(iYear,iMonth,iCellHeight,iDateTextSize){var colorTD=" bgcolor='"+gcMouseOut+"' bordercolor='"+gcMouseOut+"'";var styleTD=" valign='middle' align='center' style='height:"+iCellHeight+"px;font-weight:bolder;font-size:"+iDateTextSize+"px;";var dateCal="";dateCal+="tr";for(var i=0;i7;i++){dateCal+="td"+colorTD+styleTD+"color:#990099'"+WeekDay[i]+"/td";}dateCal+="/tr";for(var w=1;w7;w++){dateCal+="tr";for(var d=0;d7;d++){var tmpid=w+""+d;dateCal+="td"+styleTD+"cursor:pointer;' onclick='fSetSelected("+tmpid+")'";dateCal+="span id='cellText"+tmpid+"'/span";dateCal+="/td";}dateCal+="/tr";}return dateCal;}
function fUpdateCal(iYear,iMonth){var myMonth=fBuildCal(iYear,iMonth);var i=0;for(var w=1;w7;w++){for(var d=0;d7;d++){with($("cellText"+w+""+d)){parentNode.bgColor=gcMouseOut;parentNode.borderColor=gcMouseOut;parentNode.onmouseover=function(){this.bgColor=gcMouseOver;};parentNode.onmouseout=function(){this.bgColor=gcMouseOut;};if(myMonth[w][d]0){style.color=gcNotCurMonth;innerHTML=Math.abs(myMonth[w][d]);}else{style.color=((d==0)||(d==6))?gcRestDay:gcWorkDay;innerHTML=myMonth[w][d];if(iYear==giYear iMonth==giMonth myMonth[w][d]==giDay){style.color=gcToday;parentNode.bgColor=gcTodayMouseOut;parentNode.onmouseover=function(){this.bgColor=gcTodayMouseOver;};parentNode.onmouseout=function(){this.bgColor=gcTodayMouseOut;};}}}}}}
function fSetYearMon(iYear,iMon){$("tbSelMonth").options[iMon-1].selected=true;for(var i=0;i$("tbSelYear").length;i++){if($("tbSelYear").options[i].value==iYear){$("tbSelYear").options[i].selected=true;}}fUpdateCal(iYear,iMon);}
function fPrevMonth(){var iMon=$("tbSelMonth").value;var iYear=$("tbSelYear").value;if(--iMon1){iMon=12;iYear--;}fSetYearMon(iYear,iMon);}
function fNextMonth(){var iMon=$("tbSelMonth").value;var iYear=$("tbSelYear").value;if(++iMon12){iMon=1;iYear++;}fSetYearMon(iYear,iMon);}
function fGetXY(aTag){var oTmp=aTag;var pt=new Point(0,0);do{pt.x+=oTmp.offsetLeft;pt.y+=oTmp.offsetTop;oTmp=oTmp.offsetParent;}while(oTmp.tagName.toUpperCase()!="BODY");return pt;}
function getDateDiv(){var noSelectForIE="";var noSelectForFireFox="";if(document.all){noSelectForIE="onselectstart='return false;'";}else{noSelectForFireFox="-moz-user-select:none;";}var dateDiv="";dateDiv+="div id='calendardiv' onclick='event.cancelBubble=true' "+noSelectForIE+" style='"+noSelectForFireFox+"position:absolute;z-index:99;visibility:hidden;border:1px solid #999999;'";dateDiv+="table border='0' bgcolor='#E0E0E0' cellpadding='1' cellspacing='1' ";dateDiv+="tr";dateDiv+="tdinput type='button' id='PrevMonth' value='' style='height:20px;width:20px;font-weight:bolder;' onclick='fPrevMonth()'";dateDiv+="/tdtdselect id='tbSelYear' style='border:1px solid;' onchange='fUpdateCal($V(\"tbSelYear\"),$V(\"tbSelMonth\"))'";for(var i=startYear;iendYear;i++){dateDiv+="option value='"+i+"'"+i+strYear+"/option";}dateDiv+="/select/tdtd";dateDiv+="select id='tbSelMonth' style='border:1px solid;' onchange='fUpdateCal($V(\"tbSelYear\"),$V(\"tbSelMonth\"))'";for(var i=0;i12;i++){dateDiv+="option value='"+(i+1)+"'"+gMonths[i]+"/option";}dateDiv+="/select/tdtd";dateDiv+="input type='button' id='NextMonth' value='' style='height:20px;width:20px;font-weight:bolder;' onclick='fNextMonth()'";dateDiv+="/td";dateDiv+="/trtr";dateDiv+="td align='center' colspan='4'";dateDiv+="div style='background-color:#cccccc'table width='100%' border='0' cellpadding='3' cellspacing='1'";dateDiv+=fDrawCal(giYear,giMonth,dayTdHeight,dayTdTextSize);dateDiv+="/table/div";dateDiv+="/td";dateDiv+="/trtrtd align='center' colspan='4' nowrap";dateDiv+="span style='cursor:pointer;font-weight:bolder;' onclick='fSetDate(giYear,giMonth,giDay)' onmouseover='this.style.color=\""+gcMouseOver+"\"' onmouseout='this.style.color=\"#000000\"'"+strToday+":"+giYear+strYear+giMonth+strMonth+giDay+strDay+"/span";dateDiv+="/tr/tr";dateDiv+="/table/div";return dateDiv;}
with(document){onclick=fHideCalendar;write(getDateDiv());}
/script
/head
body
input type="text" style="border:1px solid #999;" onclick="fPopCalendar(event,this,this)" onfocus="this.select()" readonly="readonly" /
/body
/html
参考资料:
js怎么设置select的样式,边框和三角的颜色
select属于浏览器内置组件,标准CSS无法调整其样式。
你可以使用div来模拟select。
首先创建一个div /来模拟下拉框。
div class="mySelect"/div
然后在里面加上显示选中值的div /和模拟三角的div /以及下拉列表ul /
div class="mySelect"
div class="mySelectValue"/div
div class="mySelectDropdown"/div
ul class="mySelectOptions"/ul
/div
你可以用CSS来设置自己喜欢的样式。
接下来就是用Javascript来控制模拟的下拉框了。(这里为了方便,使用了jQuery)
// 创建临时DOM,内容为模拟的下拉框(其中省略的部分为上面写的html代码)
var $mySelect = $('div class="mySelect".../div');
// 把原来select有的样式复制到模拟的下拉框上
$mySelect.attr('class', $('#select').attr('class'));
$mySelect.attr('style', $('#select').attr('style'));
// 把原来select的选项复制到模拟的下拉框中
$('#select option').each(function () {
var value = $(this).attr('value'),
name = $(this).html();
$mySelect.find('.mySelectOptions').append('li class="mySelectOption" data-id="' + value + '"' + name + '/li');
});
// 在模拟下拉框中设置选中的值
$mySelect.find('.mySelectValue').html($('#select option:selected').html());
// 隐藏原有的select
$('#select').hide();
// 给模拟的下拉框绑定事件
$mySelect
.on('click', function (e) {
// 阻止点击事件向上冒泡
e.stopImmediatePropagation();
// 反转下拉列表的显示
$('.mySelectOptions', this).toggle();
// 给原有的select模拟点击事件
$('#select').trigger('click');
})
.on('click', '.mySelectOption', function (e) {
// 阻止点击事件向上冒泡
e.stopImmediatePropagation();
// 把选中的值显示到模拟的下拉框中
$mySelect.find('.mySelectValue').html($(this).html());
// 隐藏下拉列表
$mySelect.find('.mySelectOptions').hide();
// 把选中的值给到原来的select中
$('#select').val($(this).data('id'));
// 给原来的select模拟change事件
$('#select').trigger('change');
});
// 基本功能就到此了。其中可以缓存jQuery对象来优化,还能添加焦点事件,键盘事件等,按自己的需求慢慢修改吧。
如何通过js实现勾选复选框
js勾选复选框示例i:
//获取页面所有checkbox(checkbox的name设置一致)
var items=document.getElementByName("checkbox的name");
//遍历checkbox
for(var i=0;iitems.length;i++){
//当前checkbox实现勾选
items[i].checked=true;
}
js使用DOM设置单选按钮、复选框及下拉菜单的方法
本文实例讲述了js使用DOM设置单选按钮、复选框及下拉菜单的方法。分享给大家供大家参考。具体实现方法如下:
1.设置单选按钮
单选按钮在表单中即input
type="radio"
/它是一组供用户选择的对象,但每次只能选一个。每一个都有checked属性,当一项选择为ture时,其它的都变为false.
先贴上一个例子:
复制代码
代码如下:script
type="text/javascript"
function
getChoice()
{
var
oForm
=
document.forms["uForm1"];
var
aChoices
=
oForm.camera;
for
(i
=
0;
i
aChoices.length;
i++)
//遍历整个单选项表
if
(aChoices[i].checked)
//如果发现了被选中项则退出
break;
alert("相机品牌是:"
+
aChoices[i].value);
}
function
setChoice(iNum)
{
var
oForm
=
document.forms["uForm1"];
oForm.camera[iNum].checked
=
true;
}
/script
form
method="post"
name="uForm1"
action="addInfo.aspx"
相机品牌:
p
input
type="radio"
name="camera"
id="canon"
value="Canon"
label
for="canon"Canon/label
/p
p
input
type="radio"
name="camera"
id="nikon"
value="Nikon"
label
for="nikon"Nikon/label
/p
p
input
type="radio"
name="camera"
id="sony"
value="Sony"
checked
label
for="sony"Sony/label
/p
p
input
type="radio"
name="camera"
id="olympus"
value="Olympus"
label
for="olympus"Olympus/label
/p
p
input
type="radio"
name="camera"
id="samsung"
value="Samsung"
label
for="samsung"Samsung/label
/p
p
input
type="radio"
name="camera"
id="pentax"
value="Pentax"
label
for="pentax"Pentax/label
/p
p
input
type="radio"
name="camera"
id="others"
value="其它"
label
for="others"others/label
/p
p
input
type="submit"
name="btnSubmit"
id="btnSubmit"
value="Submit"
class="btn"
/p
p
input
type="button"
value="检测选中对象"
onclick="getChoice();"
input
type="button"
value="设置为Canon"
onclick="setChoice(0);"
/p
/form