如何选中复选框可以更改div的z-index?

时间:2013-01-19 02:06:01

标签: css checkbox

我有一个带有贝宝按钮的跨度。只有在用户确认TOS后,我必须使这个paypal按钮可用于按下。为此,我认为使用paypal按钮放置跨度另一个div与未选中的复选框,不透明度和z-index。用户检查复选框,并且叠加的div获得这样的z-index,因此带有paypal按钮的跨度将超过它并且可以被按下。

我需要像

这样的东西

HTML

<div class="with_zindex">
    <input type="checkbox​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​">
<span class="here_is_paypal_button"></span>
</div>

CSS

.with_zindex{
background-color:#fafafa;
    opacity: .5;
    filter: alpha(opacity=50);
    -moz-opacity: .5;
z-index:1000;
}
.with_zindex + input[type=checkbox]:checked {
z-index:-1000;
}

但是这对我不起作用(只有第一个格式化,不通过选中/取消选中复选框来修改)。

2 个答案:

答案 0 :(得分:0)

建议

相反,您最初可以禁用PayPal按钮,并且可以在用户点击复选框后启用它。

HTML(jQuery版本)

<input type="checkbox" name="terms" 
       onclick="$('#paypal').removeAttr('disabled');" />
<input type="image" name="paypal" id="paypal" disabled="disabled" />

HTML(JavaScript版)

<input type="checkbox" name="terms" 
       onclick="document.getElementById('paypal').removeAttribute('disabled');" />
<input type="image" name="paypal" id="paypal" disabled="disabled" />

如果你坚持使用z-index

您需要position: relative; z-index才能正常工作。并且没有否定z-index

.with_zindex {
    background-color:#fafafa;
    opacity: .5;
    filter: alpha(opacity=50);
    -moz-opacity: .5;
    position: relative;
    z-index:1000;
}
.with_zindex + input[type=checkbox]:checked {
    position: relative;
    z-index: 0;
}

您的答案

HTML

<div class="with_zindex">
    <input type="Checkbox" name="TOS" value="read">
    <span class="catItemExtraFieldsValue">
        <img src="http://gutgeordnet.de/reise/images/epaypal_de.gif">
    </span>
</div>

CSS

.with_zindex {position: relative;}

.with_zindex input[type=checkbox] {
    background-color:#fafafa;
    filter: alpha(opacity=50);
    position: absolute;
    z-index: 50;
    top: 10px;
    left: -35px;
    width: 135px;
    text-align: right;
    vertical-align: top;
    cursor: pointer;
}

.with_zindex input[type=checkbox] + .catItemExtraFieldsValue {
    position: relative;
    opacity: .5;
    -moz-opacity: .5;
    z-index: 45;
}

.with_zindex input[type=checkbox]:after {
    content: 'I accept';
    font-size: 14px;
    vertical-align: top;
    line-height: 1em;
    font-weight: bold;
    font-family: tahoma;
}

.with_zindex input[type=checkbox]:checked {position: static; display: none;}

.with_zindex input[type=checkbox]:checked + .catItemExtraFieldsValue {
    position: static;
    background-color: transparent;
    opacity: 1;
    -moz-opacity: 1;
}

小提琴:http://jsfiddle.net/praveenscience/35cQz/

答案 1 :(得分:0)

我是这样得到的:

<强> HTML:

<input class="Checkbox1" name="TOS" id="TOS" value="read" type="checkbox";"/>

<强> JS

var $K2 = jQuery.noConflict();

 $K2(document).ready(function(){

//$K2('#TOS').attr('checked', false);
//$K2('input[type=image]').attr("class", "agbunchecked");
//$K2('input[type=image]').bind("click", giveAllert);

if(!$K2('#TOS').attr('checked')) //Existenzcheck
{
    $K2('input[type=image]').attr("class", "agbunchecked");
    $K2('input[type=image]').bind("click", giveAllert);
}

function giveAllert()
{
    alert("\Pleas check the checkbox!");
    return false;
}

$K2('#TOS').change(function(event){
    if(this.checked) 
    {
        $K2('input[type=image]').attr("class", "agbchecked");
        $K2('input[type=image]').unbind("click");
    }
    else
    {
        $K2('input[type=image]').attr("class", "agbunchecked");
        $K2('input[type=image]').bind("click", giveAllert)
    }
});

  });

<强> CSS

.agbunchecked{
background-color:#fafafa;
opacity: .5;
filter: alpha(opacity=50);
-moz-opacity: .5;
}