js勾选复选框出来文本框

JavaScript019

js勾选复选框出来文本框,第1张

如下代码可以实现:

<input type="checkbox" onclick="SetInput(this,'S')" />S <input type="checkbox" onclick="SetInput(this,'M')" />M <input type="checkbox" onclick="SetInput(this,'L')" />L  <input type="checkbox" onclick="SetInput(this,'XL')" />XL

<div id="divBox"></div>

<script>

function SetInput(v, vt){

var div = document.getElementById("divBox")

if(v.checked)

div.innerHTML += "<div id='divIp"+vt+"'>"+vt+": <input type='text' value='"+vt+"' /></div>"

else

div.removeChild(document.getElementById("divIp"+vt))

}

</script>

给那些复选框绑定选择事件onchange,用jQuery去做,如:

$('input [name=check]').change(function(){//假设复选框的html:<input name="check" type="checkbox"/>

   var ele=this//当前选择的复选框,把复选框的value值显示在一个div中(<div id="show"></div>)

   var val=$(ele).val()

   var str=$('#show').html()//先获取用显示div的内容,然后把当前复选框内容拼接,再把拼接内容在show中

   str+=val//拼接当前值val

   $('#show').html(str)//显示 

})