gpt4 book ai didi

javascript - 简单的 JS 函数不起作用

转载 作者:行者123 更新时间:2023-11-30 08:35:55 25 4
gpt4 key购买 nike

我一辈子都弄不明白为什么这行不通。在单选按钮更改时更改“添加到购物车”按钮是一个简单的功能,但每次我执行该功能时,它都说这不是功能。我在这里缺少什么?

 <h2>Select a Payment Option:</h2>

<form method="post" enctype='multipart/form-data'>
<input type="radio" name="addtocart" id="fullpay" value="66" onclick="fullpay();" /><h3 style="color: #f57e20;">Full Payment - <s>$119.80</s> $79.95</s></h3>

<input type="radio" name="addtocart" id="payments" value="69" onclick="payments();" /><h3 style="color: #f57e20;">Payment plan - $26.65 / month for 3 months</h3>
<hr/>
<div id="cartbutton"></div>
</form>

JS:

 window.onload = function(){

function fullpay(){
var cartbutton = document.getElementById("cartbutton");
cartbutton.innerHTML = "<a href='/?add-to-cart=66' rel='nofollow' data-product_id='66' data-product_sku='' class='button add_to_cart_button'>Add to cart</a>";
}

function payments(){
var cartbutton = document.getElementById("cartbutton");
cartbutton.innerHTML = "<a href='/?add-to-cart=69' rel='nofollow' data-product_id='69' data-product_sku='' class='button add_to_cart_button'>Add to cart</a>";
}
};

最佳答案

您正在内部您的“加载”处理程序函数中定义您的函数。这意味着这些函数是该处理程序函数的私有(private)函数,因此它们在全局上下文中不可见。

您可以通过分配对全局对象 (window) 属性的引用来显式导出函数,或者,可能更好的是,在您的“加载”中使用 .addEventListener() "处理程序来设置事件处理而不是标记中的老式事件属性。

关于javascript - 简单的 JS 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31433950/

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