2011-12-06 97 views
0

我想顯示彈出頁面加載,不得不在彈出一個複選框字段,我想如果複選框被選中該頁面只應是可見的。問題在於它在點擊功能上運行良好,但無法使其在複選框上工作。這是我的代碼jQuery的彈出窗口的onload

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.js"></script> 

<script type="text/javascript"> 
$(document).ready(function() { 

    var id = '#dialog'; 

    //Get the screen height and width 
    var maskHeight = $(document).height(); 
    var maskWidth = $(window).width(); 

    //Set heigth and width to mask to fill up the whole screen 
    $('#mask').css({'width':maskWidth,'height':maskHeight}); 

    //transition effect  
    $('#mask').fadeIn(1000);  
    $('#mask').fadeTo("slow",0.8); 

    //Get the window height and width 
    var winH = $(window).height(); 
    var winW = $(window).width(); 

    //Set the popup window to center 
    $(id).css('top', winH/2-$(id).height()/2); 
    $(id).css('left', winW/2-$(id).width()/2); 

    //transition effect 
    $(id).fadeIn(2000);  

    //if close button is clicked 
     if ($('.window .close').attr('checked')==true){ 
    function (e) { 
    //Cancel the link behavior 
    e.preventDefault(); 

    $('#mask').hide(); 
    $('.window').hide(); 
     }};  

     //if mask is clicked 
    $('#mask').click(function() { 
     $(this).hide(); 
    $('.window').hide(); 
     });  

     }); 

    </script> 


    <div style="font-size: 10px; color: rgb(204, 204, 204);">lorem ipsum</div> 

    <div id="boxes"> 
<div style="top: 199.5px; left: 551.5px; display: none;" id="dialog" class="window"> 
Simple Modal Window | 
<input type="checkbox" class="close"> 
</div> 
<!-- Mask to cover the whole screen --> 
<div style="width: 1478px; height: 602px; display: none; opacity: 0.8;" id="mask"> </div> 
</div> 
+2

請張貼簡潔的代碼示例,因爲這將幫助那些誰是回答你的問題。 –

回答

1

嘗試閱讀本fancybox。這很棒,你可以打開彈出框並選中複選框。

0

無法completely..so你需要它,如果用戶點擊了面具消失的複選框,頁面顯示出來就好理解了你的問題?

做你的顯示在面具的頂部,因爲有一個在div..if的z-index的任何一個細節它不改變代碼

$('#mask, .close').click(function() { 
    . 
    . 

得到它的工作...

0

你應該改變你的代碼如下:

$(document).ready(function() { 

var id = '#dialog'; 

//Get the screen height and width 
var maskHeight = $(document).height(); 
var maskWidth = $(window).width(); 

//Set heigth and width to mask to fill up the whole screen 
$('#mask').css({'width':maskWidth,'height':maskHeight}); 

//transition effect  
$('#mask').fadeIn(1000);  
$('#mask').fadeTo("slow",0.8); 

//Get the window height and width 
var winH = $(window).height(); 
var winW = $(window).width(); 

//Set the popup window to center 
$(id).css('top', winH/2-$(id).height()/2); 
$(id).css('left', winW/2-$(id).width()/2); 

//transition effect 
$(id).fadeIn(2000);  

//if close checbox is checked 
    if ($('.window .close').attr('checked')==false){ 

    $('#mask').hide(); 
    $('.window').hide(); 

    }; 

    //if close checkbox is clicked 
    $('.window .close').click(function(){ 

     if(!$(this).is(":checked")){ 
     $('#mask').hide(); 
     $('.window').hide(); 
     } 

    });  

    //if mask is clicked 
    $('#mask').click(function() { 
     $(this).hide(); 
     $('.window').hide(); 
    });  

}); 

您應該添加複選框屬性「檢查」。

<input type="checkbox" class="close" checked="checked"> 
+0

這不會工作 - >怎麼是'如果($()ATTR( '檢查 ')==真' 窗口.close。'){'去工作過的唯一評價onDomReady – ManseUK

+0

你是對的。我改變了我的代碼。 –

0

它似乎是你正在尋找一個事件被觸發時,複選框被選中?

如果是這樣,則函數應該有複選框的click事件

//if close button is clicked 
    $('.window .close').click(function(){ 
     if ($(this).attr('checked')==true) 
     { 
     //Cancel the link behavior 
     e.preventDefault(); 

     $('#mask').hide(); 
     $('.window').hide(); 
     } 
    });  
0

你關閉功能是不是有效的JavaScript:

//if close button is clicked 
if ($('.window .close').attr('checked')==true){ 
    function (e) { 
    //Cancel the link behavior 
    e.preventDefault(); 

    $('#mask').hide(); 
    $('.window').hide(); 
}};  

你可以同時取出點擊活動,並與更換單個代碼塊 - >

$('#dialog .close, #mask').click(function(e){ //listen for clicks on the mask and close checkbox 
    $('#mask').hide(); // hide the mask 
    $('#dialog').hide(); // hide the dialog div 
}); 

工作示例:http://jsfiddle.net/4Mpb3/2/