【发布时间】:2014-10-31 20:26:33
【问题描述】:
我有一些 Javascript,在 PHP/WordPress 页面上的提交按钮之前添加了一些免责声明文本和一个确认复选框。我想要发生的是脚本检查 cookie 的存在。如果 cookie 不存在(或已过期),则添加免责声明文本、复选框并强制用户在继续之前单击复选框。但是一旦完成,就会写入一个 cookie,以便下次脚本运行时,如果绕过免责声明文本,复选框,只允许用户点击“提交”。
所以,类似:
if cookie-exists {
// straight to submit part of the code
} else {
// show disclaimer and checkbox
// Only allow user to hit submit if checkbox is ticked
// Set the cookie with an expire of a day
}
我可以在这里看到关于设置/读取 cookie 的答案 > How do I create and read a value from cookie?
但我只是在努力将其放入下面的代码 sn-p。
任何指针或帮助将不胜感激。谢谢。
代码 sn-p 如下:
function add_listing_select_cb()
{
?>
<script type="text/javascript">
jQuery(document).ready(function ($){
var checkbox_cont = '<br><input type="checkbox" name="I_Agree" id="I_Agree" value="I_Agree" /> <b>Disclaimer text here....</b>';
jQuery(".property-search input[type='submit']").before(checkbox_cont);
jQuery("#searchform").submit(function () {
if (!jQuery("#I_Agree").is(":checked")) {
alert("Please first agree with the terms.");
return false;
};
});
var $sel = $('#showresultsbasedonourratings'),
$opts = $sel.children();
$optsSorted = [];
$optsSorted.push($opts.eq(0));
for (var i = $opts.length - 1; i > 0; i--) {
$optsSorted.push($opts.eq(i));
};
console.log($optsSorted);
$sel.empty();
$sel.append($optsSorted);
});
</script>
<?php
}
【问题讨论】:
标签: javascript php cookies