javascript - Wordpress TinyMCE 去除 OnClick 和 OnChange(需要 jQuery)

标签 javascript jquery html wordpress

搜索了很长时间来找到解决方案,但找不到适合我需求的解决方案,所以如果我错过了某些内容,请道歉。

在我的 WordPress 网站上,我有一个带有按钮的页面,为了跟踪该按钮的链接,需要选中一个复选框。这是它的代码...

<form action="#"><input onchange="toggleAccept()" id="agreeCheckbox" type="checkbox" /><label for="agreeCheckbox">I agree to the terms</label></form>

<a href="link/?cbur=a" id="accept" onclick="mustAccept(); return false;"><img src="button.png"/></a>

头部还有一些处理此问题的代码:

    <script type="text/javascript">
function toggleAccept() {
var acceptLink = document.getElementById("accept");
var agreeCheckbox = document.getElementById("agreeCheckbox");
if (agreeCheckbox.checked) {
acceptLink.onclick=function() {
window.location=this.href + "&cbrblaccpt=true";
return false;
}
} else {
acceptLink.onclick=function() {
mustAccept();
return false;
}
}
}
function mustAccept() {
window.alert("Please check the box and agree to the payment terms of this recurring product.");
}

cbrblaccpt
  </script>

基本上,如果有人尝试单击底部而不选中该框,则会出现上面的弹出窗口。一旦他们选中该框,他们就会被带到按钮的链接。

我遇到的问题是 TinyMCE 正在删除代码的“onchange”和“onclick”部分。我猜是因为它不喜欢内联 Java 的存在。

经过大量研究后,在我看来,理想的解决方案是在单独的文件中使用 jQuery 来处理这个问题,但我完全不知道如何做到这一点。

如果有人可以在这方面提供帮助,或者提供其他解决方案,那么我会洗耳恭听。

非常感谢

最佳答案

嗯...是的,你可以用纯 jQuery 来处理它。

我给你举了一个例子:

请记住将 jQuery 库添加到您的文档中,就在 <script> 之前如果可能的话,就在 </body> 之前结束 HTML 标签 :D

jQuery:

<script>
    $(document).ready(function () {
    var agree = $("#accept"); //we cache the element, dont worry

    $("#agreeCheckbox").click(function () {
        var checked_status = this.checked;
        if (checked_status === true) {
            agree.removeAttr("disabled");
        } else {
            agree.attr("disabled", "disabled");
        }
    });
    //we convert the button into an anchor
    agree.click(function () {
        window.location = $(this).data("href") + "&cbrblaccpt=true";
        });
    });
</script>

CSS:因为我们使用按钮而不是 anchor ( <a></a> )

#accept {
    background: url(http://goo.gl/kCsKU3) center top no-repeat;
    color: #FFF;
    display: block;
    width: 200px;
    height: 44px;
    border:none;
    outline: none;
    cursor: pointer;
}
#accept:disabled {
    opacity: 0.6;
}
#accept:active {
    margin-top:2px;
}

最后,HTML:请注意,我们正在使用 data-href链接的属性而不是简单的 href因为这是一个按钮,不再是 anchor 。

<input id="agreeCheckbox" type="checkbox" />
<label for="agreeCheckbox">I agree to the terms</label>
<button data-href="link/?cbur=a" id="accept" disabled="disabled"></button>

JsFiddle:http://jsfiddle.net/n2zej2cg/

关于javascript - Wordpress TinyMCE 去除 OnClick 和 OnChange(需要 jQuery),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25753797/

相关文章:

jquery - 滚动导航栏在其他部分下消失

javascript - 如何在短代码中使用 javascript 变量?

javascript - 使用jquery读取json中的对象

jquery - 当循环动画停止时如何停止图像的轻微闪烁?

javascript - AngularJS:在 rootScope 上广播/发出的事件多次发生

javascript - 将所有 JQuery 代码添加到 Rails 项目中的 application.js 对性能有何影响

jQuery .slideToggle 在 Chrome 中完美运行,在 Firefox 中不太完美

php - 使用 $_FILES php 发送其他输入

javascript - 在 JQuery 中创建元素

c# - mvc post action json 未解析为参数