" > "> 当开关按钮发生变化时,我将使-6ren">
gpt4 book ai didi

javascript - 如何在 PHP 页面中获取 CSS 开关按钮值?

转载 作者:搜寻专家 更新时间:2023-10-31 21:22:11 25 4
gpt4 key购买 nike

嗯,

我有这段代码可以在 php while 循环中生成 CSS 开关按钮:

<div class="onoffswitch">
<input type="hidden" name="hiddenID" value="<?php echo $id; ?>">
<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch<?php echo $i ?>" <?php echo $status; ?>>
<label class="onoffswitch-label" for="myonoffswitch<?php echo $i; ?>">
<span class="onoffswitch-inner"></span>
<span class="onoffswitch-switch"></span>
</label>
</div>

当开关按钮发生变化时,我将使用 jQuery/Ajax 调用 PHP 页面。

jQuery/Ajax 代码:

$(document).ready(function () {
$(".onoffswitch").change(function() {

var hiddenID = $(this).find("[name='hiddenID']").val()
var status = $('.onoffswitch-checkbox').val();

$.ajax({
type: 'post',
url: 'changeInlineData.php',
data: {
'id' : hiddenID,
'status' : status,
},
dataType : 'html',
success: function ( result ) {
$('.validation_msg').html(result);
}
});
});
});

在这里你可以看到我只是在 php 页面中传递了 2 个键,即 idstatus。现在在 php 页面中,我只看到 id 值而不是 status 值。每次 status 键都显示 0。它应该是 0 或 1。

PHP 页面:

<?php
echo $id = (int) $_POST['id'];
echo $status = (int) $_POST['status'];

你能告诉我我做错了什么吗?

更新:

我在控制台日志网络选项卡中看到显示状态:如果我将按钮切换为打开或关闭,则始终打开!!

表单数据编号:2203状态:开启

最佳答案

您必须获取选中复选框的值。在 jquery 中更改您的状态变量,如下所示:

var status = $('.onoffswitch-checkbox:checked').val();

关于javascript - 如何在 PHP 页面中获取 CSS 开关按钮值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44736232/

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