嗯,
我有这段代码可以在 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 个键,即 id
和 status
。现在在 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/