HTML 初学->input 复选框实现单选模式,同时checkbox修改默认样式纯css改

html-css014

HTML 初学->input 复选框实现单选模式,同时checkbox修改默认样式纯css改,第1张

    初学对HTML进行了简单的学习以及记录,勿喷,只是记录一个过程,也希望对初学者有点帮助吧.

<!DOCTYPEhtml>

<html>

 label{

height : 20px

background :  chartreuse

float : none

 }

input[ type ="checkbox"]{

width :20px

height :20px

display : inline-block

text-align : center

vertical-align : middle 

line-height : 18px

position : relative}

/*未选中的状态*/

input[ type ="checkbox"]::before{

content : ""

position : absolute

top :0

left : 0

background : #fff

width : 100%

height : 100%

border : 1px solid #d9d9d9}

/*选中的状态*/

input[ type ="checkbox"]:checked::before{

content : "\2713"background-color : #fff // \2713 点击到特殊符号表

position : absolutetop : 0left : 0width :100%

border : 1px solid #e50232

color :#e50232

font-size : 20pxfont-weight : bold}

</style>

// 使用需要导入<script  language="JavaScript"src="js/ jquery.js(点击进入链接) "></script> 

  $(":checkbox").click( function (){

        $( this ). attr ("checked",true)//设置当前选中checkbox的状态为checked

       $( this ).siblings(). attr ("checked",false)//设置当前选中的checkbox同级(兄弟级)其他checkbox状态为未选中

})

function chang( id ){

var idc= '#'+id

//获取是否选中 

var isChecked= $(idc).is(":checked")

// 发送到交互的数据

window .webkit.messageHandlers.jsToOcWithPrams.postMessage({"paramid":id,"paramflag":isChecked})

}

</html>

方法有两种。

第一种通过<select>的属性来设置选中项,此方法可以在动态语言如php在后台根据需要控制输出结果。

<select  id =  "sel" >

<option  value = "1" >1</ option >

<option  value = "2"  selected = "selected" >2</ option >

<option  value = "3" >3</ option >

</ select > 

第二种为通过前端js来控制选中的项:

<script  type = "text/javascript" >

function change(){

document.getElementById("sel")[2].selected=true

}

</ script >

<select  id =  "sel" >

<option  value = "1" >1</ option >

<option  value = "2" >2</ option >

<option  value = "3" >3</ option >

</ select >

<input  type = "button"  value = "修改"  onclick = "change()"  />

获取<select>标签选中项文本的js代码为:

var  val = document.all.Item.options[document.all.Item.selectedIndex].text

var  i=document.getElementById('sel').options[document.getElementById('sel').selectedIndex].value

扩展资料

Radio 对象代表 HTML 表单中的单选按钮。在 HTML 表单中 <input type="radio">每出现一次,一个 Radio 对象就会被创建。

单选按钮是表示一组互斥选项按钮中的一个。当一个按钮被选中,之前选中的按钮就变为非选中的。当单选按钮被选中或不选中时,该按钮就会触发 onclick 事件句柄。您可通过遍历表单的 elements[] 数组来访问 Radio 对象,或者通过使用 document.getElementById()。

参考资料:百度百科-radio