为什么在 JQUERY 中的 post 之后检查所有输入?
Why all the inputs are checked after post in JQUERY?
我有无线电输入,如果我点击一个输入,然后在 post 之后,所有其他输入都变为“选中”,我不明白为什么,这是我的代码:
foreach ($tab_stickers_stored as $key => $value) {
<input class="form-check-input switch_sticker" type="checkbox" id="switch_sticker_<?=$key?>" name="switch_sticker" value="<?= $key ?>"
<?php if (isset($_POST['switch_sticker'])){echo 'checked="checked"';}?>>
}
$(".switch_sticker").on('change', function() {
var index = $(this).val();
$("input[name='switch_sticker']:checked").each(function(){
if ($("#switch_sticker_" + index).is(':checked')) {
var temp = document.getElementById('largeur_sticker_' + index).value;
document.getElementById('largeur_sticker_' + index).value = document.getElementById('longueur_sticker_' + index).value;
document.getElementById('longueur_sticker_' + index).value = temp;
} else {
var temp = document.getElementById('longueur_sticker_' + index).value;
document.getElementById('longueur_sticker_' + index).value = document.getElementById('largeur_sticker_' + index).value;;
document.getElementById('largeur_sticker_' + index).value = temp;
}
index = "";
});
});
谢谢
您的输入具有不同的 id
属性,但它们都具有相同的 name
。正是 name
决定了提交的内容,正如您在编写此行时没有意识到的那样:
<?php if (isset($_POST['switch_sticker'])){echo 'checked="checked"';}?>
if
语句中没有任何在循环中变化的内容;它每次都查看相同的值 $_POST['switch_sticker']
。
同时,JavaScript 代码基本上与问题无关,因为它只更改了各种元素的 value
。这些将显示为 $_POST['switch_sticker']
变量的值,但是因为只有一个变量和很多值,所以它只会以列表中的最后一个结束。
解决方案是让每个复选框都有自己的 name
,就像给它们自己的 value
: name="switch_sticker_<?=$key?>"
一样。然后在 PHP 中查找该名称:<?php if (isset($_POST['switch_sticker_' . $key])){echo 'checked="checked"';}?>
.
您也可以使用 something[something_else]
形式的名称,例如name="switch_sticker[<?=$key?>]"
和 <?php if (isset($_POST['switch_sticker'][$key])){echo 'checked="checked"';}?>
。这将导致 PHP 在提交时创建一个 array,这样使用起来会更好一些——你可以写 foreach ( $_POST['switch_sticker'] as $submittedKey => $submittedValue ) { ... }
.
我有无线电输入,如果我点击一个输入,然后在 post 之后,所有其他输入都变为“选中”,我不明白为什么,这是我的代码:
foreach ($tab_stickers_stored as $key => $value) {
<input class="form-check-input switch_sticker" type="checkbox" id="switch_sticker_<?=$key?>" name="switch_sticker" value="<?= $key ?>"
<?php if (isset($_POST['switch_sticker'])){echo 'checked="checked"';}?>>
}
$(".switch_sticker").on('change', function() {
var index = $(this).val();
$("input[name='switch_sticker']:checked").each(function(){
if ($("#switch_sticker_" + index).is(':checked')) {
var temp = document.getElementById('largeur_sticker_' + index).value;
document.getElementById('largeur_sticker_' + index).value = document.getElementById('longueur_sticker_' + index).value;
document.getElementById('longueur_sticker_' + index).value = temp;
} else {
var temp = document.getElementById('longueur_sticker_' + index).value;
document.getElementById('longueur_sticker_' + index).value = document.getElementById('largeur_sticker_' + index).value;;
document.getElementById('largeur_sticker_' + index).value = temp;
}
index = "";
});
});
谢谢
您的输入具有不同的 id
属性,但它们都具有相同的 name
。正是 name
决定了提交的内容,正如您在编写此行时没有意识到的那样:
<?php if (isset($_POST['switch_sticker'])){echo 'checked="checked"';}?>
if
语句中没有任何在循环中变化的内容;它每次都查看相同的值 $_POST['switch_sticker']
。
同时,JavaScript 代码基本上与问题无关,因为它只更改了各种元素的 value
。这些将显示为 $_POST['switch_sticker']
变量的值,但是因为只有一个变量和很多值,所以它只会以列表中的最后一个结束。
解决方案是让每个复选框都有自己的 name
,就像给它们自己的 value
: name="switch_sticker_<?=$key?>"
一样。然后在 PHP 中查找该名称:<?php if (isset($_POST['switch_sticker_' . $key])){echo 'checked="checked"';}?>
.
您也可以使用 something[something_else]
形式的名称,例如name="switch_sticker[<?=$key?>]"
和 <?php if (isset($_POST['switch_sticker'][$key])){echo 'checked="checked"';}?>
。这将导致 PHP 在提交时创建一个 array,这样使用起来会更好一些——你可以写 foreach ( $_POST['switch_sticker'] as $submittedKey => $submittedValue ) { ... }
.