gpt4 book ai didi

javascript - 如何在选中或取消选中时获取复选框的值而不提交表单

转载 作者:行者123 更新时间:2023-12-01 02:28:23 24 4
gpt4 key购买 nike

我想在有人选中或取消选中复选框时更改值。这是我的代码:

<?php 
if($params['status'] == "2"){
?>
<div class="pull-right" id="confirm-ship">
<input type="checkbox" class="chkone"><br>
if(checkbox == check){
<span>Confirmed</span>
}else{
<span>Not Confirmed</span>
}
</div>
<?php
}
?>

当有人单击复选框而不提交页面或重新加载页面时,我想显示已确认或未确认的消息。有可能吗,有人帮助我吗?

我刚刚在上述场景中添加了以下代码,只是为了提供一个想法。

if(checkbox == check){
<span>Confirmed</span>
}else{
<span>Not Confirmed</span>
}

最佳答案

首先,您需要设置一个事件监听器来监听更改事件并触发函数。

如果你给出你的元素 id您可以更轻松地定位该元素。

您还需要将 javascript 放入 <script>标签。有些人将它们放在页面正文或最底部。就我个人而言,我喜欢将我的放在<head>中标记为使用 window.onload只会尝试 getElementById()当页面加载时。

window.onload=function(){ // Trigger when the page is ready
//Set change event
document.getElementById('Confirmation').addEventListener('change', Confirm, false);
}

function Confirm(){
var Confirmation=document.getElementById('Confirmation').checked;
var Message;
if(Confirmation){
Message="Confirmed";
}else{
Message="Not Confirmed"
}
document.getElementById('Result').innerHTML=Message;
}
<input type="checkbox" id="Confirmation" />
<span id="Result">Not Confirmed</span>

如果您对上述源码有任何疑问,请在下面留言,我会尽快回复您。

关于javascript - 如何在选中或取消选中时获取复选框的值而不提交表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32563615/

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