gpt4 book ai didi

javascript - 使用 js 从 Bootstrap 模式获取 cookie 的值

转载 作者:行者123 更新时间:2023-12-03 02:39:36 25 4
gpt4 key购买 nike

我正在尝试创建一个有两个选项的模式:是或否。当用户选择 no 时,我将创建一个条件 if value=no then 不显示模态。

问题是我无法完成它。如果答案是肯定的,我可以操纵该信息,我需要显示一个询问问题的模式,获取用户的答案,并基于此每 15 天做一些事情。

我的代码目前无法正常工作

<script>
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
var name = cname + "=";
var decodedCookie = decodeURIComponent(document.cookie);
var ca = decodedCookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}

function checkCookie() {

$('#hewant').on('click', function() {
var valyes = $(this).text();
console.log(valyes);
});

$('#hedont').on('click', function() {
var valno = $(this).text();
console.log(valno);
});
var user = getCookie("username");

if (user != "") {
alert("Welcome again " + user);
} else {
$("#myModalcashout").modal();
user = "testing me";
if (user != "" && user != null) {
setCookie("username", user, 15);
}
}
}
</script>

<body onload="checkCookie()">

<div class="modal fade" id="myModalcashout">
<div class="modal-dialog">
<!-- for modal to active manually->
<!-- Modal content-->
<div class="modal-content">

<div class="modal-body">

<p style="text-align:center">test</p>
<p style="text-align:center">question</p>
<p style="text-align:center">for u</p>
<div id="hewant" value="yes"> yes i am </div>
<div id="hedont" value="no"> no i dont</div>
</div>

</div>

</div>
</div>
</body>

最佳答案

我为你重组了你的代码。对于初学者来说,两个按钮只需要 1 个监听器,可以使用模式中按钮上的 class 进行分配(我将其从 div 更改为 button标签。

注意:“运行此代码片段”在这个沙盒环境中对于 cookie 操作不起作用,因此您必须自己进行测试。

现在有一个 showModal() 函数,您将在加载时调用该函数,该函数负责显示模态并将单击监听器附加到模态上的两个按钮。

当用户在模式上单击"is"或“否”时,我们会得到他们的答案并将其传递给 checkCookie() 函数,该函数会按照您的要求执行操作。

我用经过测试和工作的函数替换了您的创建和设置cookie函数(还添加了eraseCookie()),关于所有3个函数有一个很好的解释Here

showModal();

function showModal() {
// only show modal if cookie for the user is null
var user = readCookie("username");
if (user != null) {
alert("Welcome again " + user);
}
else {
user = "testing me";
$("#myModalcashout").modal();

// add listener on buttons
$('.modalButton').on('click', function() {
$('#myModalcashout').modal().hide();
var answer = $(this).val();
console.log(answer);

// create a cookie with the users answer to the modal
createCookie("username", answer, 15);
});
}
}

function createCookie(name, value, days) {
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
var expires = "; expires=" + date.toGMTString();
} else var expires = "";
document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}

function eraseCookie(name) {
createCookie(name, "", -1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://npmcdn.com/tether@1.2.4/dist/js/tether.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>


<div class="modal fade" id="myModalcashout">
<div class="modal-dialog">
<!-- for modal to active manually->
<!-- Modal content-->
<div class="modal-content">
<div class="modal-body">
<p style="text-align:center">test</p>
<p style="text-align:center">question</p>
<p style="text-align:center">for u</p>
<button class="modalButton" value="yes"> yes i am </button>
<button class="modalButton" value="no"> no i dont</button>
</div>
</div>
</div>
</div>

关于javascript - 使用 js 从 Bootstrap 模式获取 cookie 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48388596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com