到期时禁用按钮

时间:2015-11-19 09:48:11

标签: javascript php jquery

我有一个按钮,如果优惠券已经下载或已过期,它会提醒用户。如何实现,如果它已过期,而不是返回警报,它会禁用该按钮?目前以下是我的按钮。

<?php
    if (isset ( $entry["couponId"] )) { 
    if(! isset($_SESSION["loginSuccess"])) { ?>
    <button type="submit" value="Coupon" class="disabledbuttonlink" id="couponButton-<?php echo $entry['id']; ?>">Log in to download</button>
    <?php
    } else {
?>
    <button type="submit" value="Coupon" class="buttonlink" id="couponButton-<?php echo $entry['id']; ?>">Coupon</button>
<?php } }?>

以下是我的剧本

<script type="text/javascript">
<?php
    foreach($resultAllPosts['lists'] as $entry){?>
        $('#couponButton-<?php echo $entry['id']; ?>').click(function(){
            if ("<?php echo $sessionId?>" == "<?php echo $non_login_user?>") {
            alert("Please log in to download coupons");
            } else {
    <?php
        $fields = array(
            'projectId' => $PROJECT_ID,
            'sessionId' => $sessionId,                      
            'promotionId' => $entry['couponId']
        );

    $downloadedCoupon = get_decoded_info( $SERVER_URL, $PORT, 'tos-member/user/membershipcoupon/download' , $fields );

    if($downloadedCoupon['message'] != ('Same CouponId downloaded before.' || 'Coupon is expired.' || 'Invalid Coupon.')){              
                    ?>
    window.location = "<?php echo $base_url; ?>coupon-singleview?id=<?php echo $entry["couponId"];?>";
    <?php           
    } else {
    if($downloadedCoupon['message'] == 'Same CouponId downloaded before.'){
    echo "alert('Coupon has already been downloaded');";    ?>
    <?php } else if ($downloadedCoupon['message'] == 'Coupon is expired.') {
    echo "alert('Coupon is expired');";
    } else {
    echo "alert('Invalid coupon.');";
            }
        }
    ?>
    }});
    <?php }?>
</script>

我已尝试以下方法通过javascript禁用该元素,但事实并非如此 工作

...
<?php } else if ($downloadedCoupon['message'] == 'Coupon is expired.') {?>
    document.getElementById('couponButton-<?php echo $entry['id']; ?>').disabled = true;
<?php } else {
...

1 个答案:

答案 0 :(得分:1)

使用JQuery,您可以更改禁用的属性。

$('#couponButton').prop("disabled", true);