您的位置:首页技术文章
文章详情页

javascript - 怎么简写这段jQuery功能?

【字号: 日期:2023-05-29 09:53:07浏览:34作者:猪猪

问题描述

<!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:

你这是要做什么功能啊,没看明白。。。。。。

标签: JavaScript
相关文章: