猿问

按钮 Onclick 更改值,永久直到下次单击 Jquery

我正在创建一个购物车,其中,如果用户单击“添加到购物车按钮”,该按钮应更改为“继续购物”,它应该是永久性的(即使在刷新页面之后)。如果再次点击按钮,它应该变回“添加到购物车”。


我有这个代码,但刷新后它会改变


    <script>

        $('.add').click(function() {

            this.value = 'Continue shopping';

        });

    </script>

我使用 php 作为后端。


慕的地10843
浏览 232回答 3
3回答

吃鸡游戏

反映数据库中的变化。尝试在数据库中创建 bool 列并尝试使用 ajax 在数据库中更改它<script>$('.add').click(function() {&nbsp; &nbsp; this.value = 'Continue shopping';&nbsp; &nbsp;$.ajax({&nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp;url: "currentfilename.php",&nbsp; &nbsp; &nbsp;data: {change:'true'},&nbsp; });});</script>PHP文件&nbsp;if(isset($_POST['change'])){&nbsp;$connection= mysqli_connect("localhost","my_user","my_password","my_db");&nbsp;$sql="update tablename set colname=1";&nbsp;mysqli_query($connection,$sql);}&nbsp;HTML文件写入选择查询并获取列并写入以下内容if($row['col']==1){echo"<button type='button'>Continue shopping</button>&nbsp;}

守着一只汪

这是一个解释一切的教程。你没有要求每次看到教程,但它是一个很好的阅读。https://phppot.com/php/simple-php-shopping-cart/

回首忆惘然

从数据库和视图文件中获取值。&nbsp;<?php&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$htmlResponse = "";&nbsp; &nbsp; //by which column values you want change button text check condition for that.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($data->column_name == 0) {&nbsp; &nbsp; &nbsp;$htmlResponse .= '<span onclick="changeActivityStatus(' . $data->id . ', 1, this)" style="cursor: pointer;">add to cart</span>';&nbsp; &nbsp; &nbsp; &nbsp; }else {&nbsp; &nbsp; &nbsp;$htmlResponse .= '<span onclick="changeButton(' . $data->id . ', 0, this)" data-value="Blocked" style="cursor: pointer;">Continue shopping</span>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;?>&nbsp; <?php echo $htmlResponse; ?>call function in js file//test.php file is just an example you put here your php file namefunction changeButton(id, status) {&nbsp; &nbsp; $.post(base_url + "test.php", {id: id, status: status}, function (data) {&nbsp; &nbsp; &nbsp; &nbsp; if (data.code === "1") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alert('success');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.location.reload();&nbsp; &nbsp; &nbsp; &nbsp; } else if (data.code === "0") {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;alert('something went wrong');&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });}在 test.php 中,如果单击添加到购物车按钮值将更新并且按钮文本将更改,则您必须使用更新查询
随时随地看视频慕课网APP
我要回答