2016-11-26 44 views
0

我使用TIS代碼:jQuery的任何東西,但格,一切單擊INSIDE

$(document).bind('click', function(e) { 
    if(!$(e.target).is('#paybox')) { 
    $('#paybox').hide(); 
    } 
}); 

隱藏#paybox當用戶點擊任何地方,但#paybox

但是,當我點擊 「廣播」 的形式:

<div id="paybox"> 
    <table width="100%"> 
     <tr> 
      <form method="post" action=""> 
       <td> 
        <input type="radio" name="dauer" value="small" checked> 
       </td> 

       <td> 
        <input type="radio" name="dauer" value="mid"> 
       </td> 

       <td> 
        <input type="radio" name="dauer" value="big"> 
       </td> 
      </form> 
     </tr> 
    </table> 
</div> 

#payboy然後#payboy得到隱患!

我該如何預防?

+0

的孩子解釋你的要求 –

回答

3

您應該檢查您的點擊是否在paybox內或沒有。像這樣的東西應該工作:

​​

所以這只是隱藏#paybox元素如果點擊既不是對paybox也不是說是#paybox元素中的任何節點。

+1

您最好使用'$(e.target).closest('#paybox')。length',因爲'nearest()'ch ecks元素本身,以及它的祖先 – billyonecan

+0

@billyonecan是的,這個問題似乎已經在這裏得到了回答http://stackoverflow.com/questions/152975/how-do-i-detect-a-click-outside-an - 元素#答案-3028037 – bummzack

1

您可以使用closestparents檢查這個如下

$(document).bind('click', function(e) { 
 
    if(!$(e.target).is('#paybox') && $(e.target).closest('#paybox').length === 0) { 
 
    $('#paybox').hide(); 
 
    } 
 
});
#paybox { 
 
    background-color: gray; 
 
    height: 100px; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<div id="paybox"> 
 
    <table width="100%"> 
 
     <tr> 
 
      <form method="post" action=""> 
 
       <td> 
 
        <input type="radio" name="dauer" value="small" checked> 
 
       </td> 
 

 
       <td> 
 
        <input type="radio" name="dauer" value="mid"> 
 
       </td> 
 

 
       <td> 
 
        <input type="radio" name="dauer" value="big"> 
 
       </td> 
 
      </form> 
 
     </tr> 
 
    </table> 
 
</div>

0

你可以檢查,如果目標是你的div

// if target is not paybox and is not child of paybox 
$(document).bind('click', function(e) { 
    if(!$(e.target).is('#paybox') && !$(e.target).parents('#paybox').length) { 
    $('#paybox').hide(); 
    } 
}); 
相關問題