简体   繁体   English

尽管自动刷新页面,但单击时仍隐藏跨度

[英]hide span when i click on it despite the automatic page refresh

html (quotazioni.php) html(quotazioni.php)

<?php 
    $azioni = $db_handle->runQuery("SELECT idAzione,nome,prezzo FROM azioni");
    foreach($azioni as $azione){
?>          

                        <tr>
                            <td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><a href="azione.php?idAzione=<?=$azione["idAzione"]?>"><?php echo $azione["nome"]; ?></a></td>
                            <td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><?php echo $azione["prezzo"]; ?></td>
                            <td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
                                <a href="compraVendi.php?action=segui&idAzione=<?=$azione["idAzione"]?>" id="<?=$azione["nome"]?>" style="text-decoration:none;" class="preferiti" >
                                    <span style="color:yellow;font-size:200%;" id="<?=$azione["idAzione"]?>" >&star;</span>
                                </a>
                            </td>
                        </tr>
<?php 
    }
?>

php (compraVendi.php) php(compraVendi.php)

case "segui":
            $db_handle->query("INSERT INTO preferiti (visibile,idAzione) VALUES (1,'".$_GET["idAzione"]."')");  
            header("location: quotazioni.php");
            break;

javascript JavaScript的

    $(document).ready(function(){
    <?php
        $preferiti = $db_handle->runQuery("SELECT * FROM preferiti");
        foreach($preferiti as $preferito){
            if ($preferito["visibile"]==1){ 
    ?>          
            var element = document.getElementById(<?=$preferito["idAzione"]?>);
            element.hide();
    <?php
            }
        }
    ?>
});

I must hide the span inside the link after I click on it. 单击链接后,必须将链接内的跨度隐藏起来。 How do I keep the span disabled considering the page contains an automatic refresh? 考虑页面包含自动刷新,如何保持跨度禁用? I provide an example of code which not work, please help me to solve the problem. 我提供了一个无法正常工作的代码示例,请帮助我解决问题。 In the sql database, the table preferiti contains idPreferito,visibile and idAzione. 在sql数据库中,表preferrediti包含idPreferito,visibile和idAzione。 The row preferito contains 1 if i clicked on the respective prefer. 如果我单击相应的首选项,则首行包含1。

When user clicks on the the star - you need to save this data to a persistent storage, e,g your backend. 当用户点击星号时-您需要将此数据保存到持久存储中,例如您的后端。 This is the only way you will be able to regain this state after page refresh. 这是刷新页面后能够重新获得此状态的唯一方法。

So when serving the page to the client, you will consider another field, for example 'disabled' which contains string 'disabled' 所以服务页面给客户端的时候,你会考虑其他领域,例如'disabled'包含字符串“已禁用”

It could be something like this: 可能是这样的:

<a href="#" id="<?=$azione["nome"]?>" class="preferiti" >
    <span class="favourites-star <?=$azione["disabled"]?>" id="<?=$azione["idAzione"]?>" >&star;</span>
</a>

Consider not using insline styles and instead using classes for styling - this is a general good practice. 考虑不使用内联样式,而是使用类进行样式设置-这是一种良好的常规做法。

.favourites-star {
    color:yellow;
    font-size:200%;
}

Classes are also better when dealing with events. 在处理事件时,类也更好。

$(document).ready(function(){
    $('.prefereti').on('click', function(evt){
        // Save the state first and then disable the star - try it yourself!
        $.post()
            .done(function () {
                 $(evt.currentTarget).addClass('disable');
            })
            .fail(function () {
                 // Don't disable - instead show error
            });
    });
});

One more thing that you need to keep in mind while the state is being saved, your page might reload - so you might like to prohibit it from reloading for that time. 保存状态时,还需要记住一件事,页面可能会重新加载-因此,您可能希望在此期间禁止重新加载页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM