English 中文(简体)
多种成分的抽样功能。 我如何巩固
原标题:Same jQuery function for multiple form elements. How do I consolidate

如果对多个检查箱进行检查,我就需要展示和隐藏一片。

需要显示的每块 d子将与检查箱名称相同。 如果不书写处理每个检查箱的职能,是否容易做到这一点?

    $( input[name=checkbox_1] ).click(function() {
        if ($( input[name=checkbox_1] ).is( :checked )) {
            $( #checkbox_1 ).show( slow );
        }

        else {
            $( #checkbox_1 ).hide( slow );
        }
     });
     $( input[name=checkbox_2] ).click(function() {
                   if ($( input[name=checkbox_2] ).is( :checked )) {
            $( #checkbox_2 ).show( slow );
        }

        else {
            $( #checkbox_2 ).hide( slow );
        }
    });

  });
最佳回答

它像你一样,有一些命名公约。 你们只能接触到当前的背景目标,获得姓名,并从事您的公约工作。 您不妨向您的检查箱提供一些特殊学科,以便于挑选:

$( input.someCssClass ).click(function() {

    var selector =  #  + $(this).attr( name );

    if ($(this).is( :checked ))
        $(selector).show( slow );
    else
        $(selector).hide( slow );
});
问题回答

你们可以从名字中获得该身份证明。

$( input[name^=checkbox_] ).click(function() {
  $( #  + this.name)[this.checked ?  show  :  hide ]( slow );
});

使用,获取投入,然后使用><>。 如核对,将<代码>.show(缓慢),否则,.hide(缓慢)

$( input[name^=checkbox] ).click(function() {
    if(this.checked){
       $( #  + this.name).show( slow );
    } else{
       $( #  + this.name).hide( slow );
    }

}

Edit:与他人一样,使用挑选人开端比检查指数Of更好。

您可使用<代码>toggle(<>t/code>,而不是if ......否则,以进一步削减代码:

( input[name^=checkbox_] ).click(function(){
    $( #  + this.name).toggle( slow )
});

Toggle还可以在被点击时发挥不同功能,而不是仅仅躲藏/how取分子:

( input[name^=checkbox_] ).toggle(function(){
    $( #  + this.name).hide( slow )
}, function(){
    $( #  + this.name).show( slow )
    } );




相关问题
getGridParam is not a function

The HTML: <a href="javascript:void(0)" id="m1">Get Selected id s</a> The Function: jQuery("#m1").click( function() { var s; s = jQuery("#list4").getGridParam( selarrrow )...

selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

jQuery cycle page with links

I am using the cycle plugin with pager functionality like this : $j( #homebox ) .cycle({ fx: fade , speed: fast , timeout: 9000, pager: #home-thumbs , ...

jquery ui dialog opens only once

I have a button that opens a dialog when clicked. The dialog displays a div that was hidden After I close the dialog by clicking the X icon, the dialog can t be opened again.

jConfirm with this existing code

I need help to use jConfirm with this existing code (php & Jquery & jAlert). function logout() { if (confirm("Do you really want to logout?")) window.location.href = "logout.php"; } ...

Wrap text after particular symbol with jQuery

What I m trying to do, is wrap text into div inside ll tag. It wouldn t be a problem, but I need to wrap text that appears particularly after "-" (minus) including "minus" itself. This is my html: &...

热门标签