gpt4 book ai didi

jQuery noobie 无法使选中的复选框显示警报

转载 作者:行者123 更新时间:2023-11-28 05:08:59 25 4
gpt4 key购买 nike

我之前找到了这个答案,如果按下按钮但未选中复选框,则会发出警报。

为什么这行不通?

我脑子里有这个

<script src="http://code.jquery.com/jquery-latest.js"></script>

在正文中:

<input value="1"  type="checkbox" name="salgsvilkar" ID="checkbox2"  style="float:left;"
onclick="document.getElementById('scrollwrap').style.cssText='border-color:#85c222; background-color:#E5F7C7;';" /><label for="checkbox2" class="akslabel">Salgs og leveringsvilkår er lest og akseptert</label>
</span>

{literal}
<script type="text/javascript">
$(function() {
//checkbox
$("#checkbox2").click(function(){
//if this...
//alert("this")...
if($("#checkbox2").is(':checked'))
{
alert("im checked");
}
});
//button
$("#fullfor_btn").click(function(e){
if(!$("#checkbox2").is(':checked'))
{
alert("you did not check the agree to terms...");
e.preventDefault();
}
});
}
</script>
{/literal}

这在另一个 .tpl 上:

<label></label>
<button type="submit" class="submit" name="{$method}" id="fullfor_btn" title="Fullfør bestillingen nå" value="">&nbsp;</button>

可能出了什么问题? jQuery 根本不会激发任何东西。

更新 id terms 改成了checkbox2,还是没有反应。

最佳答案

您的复选框的 ID 为“checkbox2”,但您正尝试在按钮的点击处理程序中使用 ID“terms”访问它。

更新

您缺少 $(function(){...}) 调用的右括号。这会在加载页面时引发语法错误,并且脚本 block 中的代码永远不会运行。更改脚本 block 中的最后一行:

}

到:

});

我已经用这个更改测试了代码,它工作正常。

关于jQuery noobie 无法使选中的复选框显示警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2711849/

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