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 - 关于js高级程序中的问题2. mysql - 要取出数据库中按某字段排序后的前10,20,30条数据要怎么做?3. python - 怎么进行服务器性能分析4. css - 如何让子元素不受父元素overflow的影响5. javascript - CSS图片轮播显示问题6. thinkPHP5中获取数据库数据后默认选中下拉框的值,传递到后台消失不见。有图有代码,希望有人帮忙7. windows-7 - win7下使用cmder,如何设置vim的tab为4个空格?8. python 多进程 或者 多线程下如何高效的同步数据?9. MySQL的SELECT...FOR UPDATE究竟起什么作用10. word-wrap该如何使用?

网公网安备