码迷,mamicode.com
首页 > 其他好文 > 详细

checkbox:click事件触发文本框显示隐藏

时间:2017-09-10 13:32:40      阅读:241      评论:0      收藏:0      [点我收藏+]

标签:play   set   北京   blog   for   route   ntb   style   utf-8   

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>checkbox</title>
    <script src="jquery.js"></script>
</head>
<body>
<form action="#" id="form">
    <input type="checkbox" name="city" value="南京">南京
    <input type="checkbox" name="city" value="北京">北京
    <input type="checkbox" name="city" value="纽约">纽约
    <input type="checkbox" name="city" value="其他" id="other" onclick="change()">其他
    <input type="text" style="display: none" id="txt">
</form>
<script>
//    jQuery
//    $(function () {
//        $("#other").click(function () {
//            if($(this).attr("checked")){
//                $("#txt").css("display","block")
//            }else {
//                $("#txt").css("display","none")
//            }
//        })
//    })
//    原生JS
function change() {
    var other = document.getElementById("other");
    var txt = document.getElementById("txt");
    if (other.checked == true) {
        txt.style.display = "block";
    }else{
        txt.style.display = "none";
    }
}
</script>
</body>
</html>
如果用js改变checkbox的值,onchange不会被触发。
事件一般是监听用户的动作,用js改不属于用户动作。
另:document.getElementById("otherRoute").checked=true;
都在js改变checkbox的值了,在这句的后面就执行onchange方法的内容不就行了,就酱

checkbox:click事件触发文本框显示隐藏

标签:play   set   北京   blog   for   route   ntb   style   utf-8   

原文地址:http://www.cnblogs.com/sunxirui00/p/7500646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!