javascript - 怎么简写这段jQuery功能?
问题描述
<!DOCTYPE html><html lang='zh'><head> <meta charset='UTF-8' /> <meta name='viewport' content='width=device-width, initial-scale=1.0' /> <meta http-equiv='X-UA-Compatible' content='ie=edge' /> <title>Document</title> <script src='http://code.jquery.com/jquery-2.1.1.min.js'></script> <style>.bd {display: none;} </style></head><body> <input type='checkbox' name='purpose_id[]' value='13'>家庭装饰 <input type='checkbox' name='purpose_id[]' value='17'>礼物馈赠 <p class='bd'>1放家庭装饰 input</p> <p class='bd'>2放礼物馈赠 input</p><script>$(function(){ var onff0 = true; var onff1 = true; $(’.purpose:eq(0):checkbox’).on(’click’,function(){if (onff0) { $(’.bd:eq(0)’).css(’display’,’block’);} else { $(’.bd:eq(0)’).css(’display’,’none’);}onff0 = !onff0; }) $(’.purpose:eq(1):checkbox’).on(’click’,function(){if (onff1) { $(’.bd:eq(1)’).css(’display’,’block’);} else { $(’.bd:eq(1)’).css(’display’,’none’);}onff1 = !onff1; })}) </script> </body></html>
问题解答
回答1:$('.purpose').change(function(){ var index = $(this).index(); if($(this).is(':checked')){$('.bd').eq(index).show(); }else{ $('.bd').eq(index).hide(); }});回答2:
首先给bd的p加上data-v对应上面的value
<p data-v='13'>1放家庭装饰 input</p><p data-v='17'>2放礼物馈赠 input</p>$(’.purpose’).on(’click’,function(){ var $bd = $(’p[data-v=’ + $(this).val() + ’]’); if($bd.is(':hidden'))$bd.show(); else$bd.hidden();})回答3:
<!DOCTYPE html><html lang='zh'><head> <meta charset='UTF-8' /> <meta name='viewport' content='width=device-width, initial-scale=1.0' /> <meta http-equiv='X-UA-Compatible' content='ie=edge' /> <title>Document</title> <script src='http://code.jquery.com/jquery-2.1.1.min.js'></script> <style>.bd {display: none;} </style></head><body> <input type='checkbox' name='purpose_id[]' value='0'>家庭装饰 <input type='checkbox' name='purpose_id[]' value='1'>礼物馈赠 <p class='bd'>1放家庭装饰 input</p> <p class='bd'>2放礼物馈赠 input</p><script>$(function(){ $(’.purpose:checkbox’).on(’click’,function(e){ $(’.bd:eq(’ + e.target.value + ’)’).css(’display’, e.target.checked ? ’block’ : ’none’); })}) </script></body></html>回答4:
$(function(){ $.each($('input[name=’purpose_id[]’]'), function(index) {$(this).click(function(){ if($(this).prop('checked')){$('.bd').eq(index).show(); }else{$('.bd').eq(index).hide(); }}) });})回答5:
p加个属性跟复选框能对应,那个通过index来进行判断的写法位置稍微一改动就没用
<input type='checkbox' name='purpose_id[]' value='13'>家庭装饰<input type='checkbox' name='purpose_id[]' value='17'>礼物馈赠<p id='13'>1放家庭装饰 input</p><p id='17'>2放礼物馈赠 input</p><script> $(function () {$(’input[type=checkbox]’).on(’click’, function () { var $p = $('#'+ $(this).val()); $(this).attr('checked') ? $p.show() : $p.hide();}) })</script>回答6:
$('.purpose').on(’change’,function(){
var index = $(this).index(); if($(this).is(':checked')){ $('.bd').eq(index).show(); }else{ $('.bd').eq(index).hide(); }
});
回答7:<input type='checkbox' name='purpose_id[]' onchange='check1();' data='1' value='13'>家庭装饰
<input type='checkbox' name='purpose_id[]' onchange='check2()' data='2' value='17'>礼物馈赠<p class='bd'>1放家庭装饰 input</p><p class='bd'>2放礼物馈赠 input</p><script> function check1(){$(’.bd:eq(0)’).toggle(); } function check2(){$(’.bd:eq(1)’).toggle(); }</script>回答8:
你这是要做什么功能啊,没看明白。。。。。。
相关文章:
1. javascript - 把字符串日期转换成一般时间格式后,发现小于10的数字不会自动添加0在前面,如下2. python相关问题求解决,有偿3. HTML5怎么访问本地文件?4. javascript - js判断一个数组是否重复5. 提示我模板文件不存在: tp5 application index view body.html6. javascript - prismplayer播放器全屏问题7. javascript - vue2.0中router.repalce跟router.push有什么区别,二者的应用场景是会有怎样的区别?8. javascript - 在sublime中如何给.vue文件注释?9. javascript - input输入框触发change事件后,外部元素会被选中10. javascript - 这种上传图片预览怎么做?