0

无论选择什么,它仍然为所有复选框分配值 1,而不是将选定的复选框更改为值 0。从语法的角度来看,这是正确的代码,但无论我不这样做,默认为 1看看为什么它没有将选定的框值更改为“0”

//Correct answer variables
$chkBox1 = '1';
$chkBox2 = '1';
$chkBox3 = '1';
$chkBox4 = '1';
$chkBox5 = '1';

if (isset($_POST['chkBox1'])) {

if ($chkBox1 == 'chkBox1Selected') {
$chkBox1 = '0';
}

}//End of chkBox1Selected logic


if (isset($_POST['chkBox2'])) {

if ($chkBox2 == 'chkBox2Selected') {
$chkBox2 = '0';
}

}//End of chkBox2Selected logic


if (isset($_POST['chkBox3'])) {

if ($chkBox3 == 'chkBox3Selected') {
$chkBox3 = '0';
}

}//End of chkBox3Selected logic
4

1 回答 1

4

您的if陈述永远不会评估为true.

看一个复选框:

$chkBox1 = '1';

if (isset($_POST['chkBox1'])) {
    if ($chkBox1 == 'chkBox1Selected') {
        $chkBox1 = '0';
    }
}

$chkBox1设置为'1',并且它永远不会改变,所以它永远不会等于'chkBox1Selected'

话虽如此,您不必担心复选框的值,因为只有选中的复选框才会发送到服务器

从理论上讲,您可以这样做:

if (isset( $_POST['chkBox1'])) {
    $chkBox1 = '0';
}

但是,如果您想读取复选框的值,您应该可以这样做:

if (isset( $_POST['chkBox1'])) {
    if ($_POST['chkBox1'] == 'chkBox1Selected') {
        $chkBox1 = '0';
    }
}

或者,更简洁地说:

if( isset( $_POST['chkBox1']) && ($_POST['chkBox1'] == 'chkBox1Selected')) {
        $chkBox1 = '0';
}
于 2012-07-02T16:53:28.240 回答