这是复选框:

$qry_strings4 = "SELECT * FROM `Y new questions`";
$preps4 = $pdo_conn->prepare($qry_strings4);
            $preps4->execute();
           // $row = $preps4->fetch(PDO::FETCH_ASSOC);
        //echo "$count";
            echo "<table style='border:0px; background-color:lightgrey; width:75%'><thead style='border:0px;'><tr style='border:0px solid white; background-color:#153E7E; text-align:left; color:white; padding: 5; margin: 5;'><th style='border:1px white; padding: 5; margin: 5;'>Question</th><th style='border:1px white; padding: 5; margin: 5;'>Response</th></tr></thead><tbody>";
            while ($row = $preps4->fetch(PDO::FETCH_ASSOC)) {
                echo "<tr style='border:1px white; background-color:lightgrey; color:black; padding: 5; margin: 5;'><td style='border:1px white; vertical-align:top; padding: 5; margin: 5;'>{$row['starName']}</td>
                      <td style='border:1px white; padding: 5; margin: 5;'><div id='wrap'>
<textarea cols='85' rows='2' name='question' id='{$row['questionID']}' class='response textbox'>{$row['question']}</textarea>";
echo "YES: <input type='checkbox' name='yes' value='yes'>
NO: <input type='checkbox' name='no' value='no'>";
            }
            echo "</tbody></table>";
            echo "<button type='button' class='save_btn' style='align:right'>Save All</button><br>";


这是js:

$(function(){
  $(".save_btn").on('click', function(){
    //var check = $("input[no]").is(":checked")?2:1;
    var check = $("input[name='no']").is(":checked") ? 2 : 1;
    var questionID = $("textarea").attr('id');
    var question = $("textarea").val();
    $.post("response14.php",{
        //"questionID":$("textarea[name=question]").attr('id'),
        //"question":$("textarea[name=question]").html(),
        "questionID":questionID,
        "question":question,
        "approved":check
    });
      alert("saved");
      location.reload();
    });
  });


这是response14.php:

include("db_conn.php");
$sql = "update questions set approved = ?, question = ? where questionID = ?";
$qc = $pdo_conn->prepare($sql);
$qc->execute(array($_POST['approved'], $_POST['question'], $_POST['questionID']));
echo 'Saved<br>';


这是渲染的html:

<textarea cols='85' rows='2' name='question' id='3792' class='response textbox'>no</textarea>YES: <input type='checkbox' name='yes' value='yes'>
NO: <input type='checkbox' name='no' value='no'><tr style='border:1px white; background-color:lightgrey; color:black; padding: 5; margin: 5;'><td style='border:1px white; vertical-align:top; padding: 5; margin: 5;'>Gavin Casalegno</td>
                      <td style='border:1px white; padding: 5; margin: 5;'><div id='wrap'>
<textarea cols='85' rows='2' name='question' id='3793' class='response textbox'>yes</textarea>YES: <input type='checkbox' name='yes' value='yes'>
NO: <input type='checkbox' name='no' value='no'></tbody></table><button type='button' class='save_btn' style='align:right'>Save All</button>


应该在勾选“是”复选框时显示1,在勾选“否”复选框时显示2,但是它始终显示1-现在已修复

但是现在我不确定要编辑第二个数据库行而不是第一个数据库行,因为有多个数据库行可供选择,并且我目前只能编辑第一个数据库行而不是第二个数据库行,因此在上面添加了更多代码显示这个

最佳答案

我会在标记中更改名称属性,例如skip name='no[]',这不是必需的/无用的:

YES: <input type='checkbox' name='yes' value='yes'>
NO: <input type='checkbox' name='no' value='no'>";


并使用更准确的选择器:

var check = $("input[name='no']").is(":checked") ? 2 : 1;


并测试:

$('input[type="checkbox"]').click(function() {
    var check = $("input[name='no']").is(":checked") ? 2 : 1;
    console.log(check);
});

关于javascript - var check = $(“input [no]”)。is(“:checked”)?2:1;以及如何到达第二数据库行,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18904857/

10-16 13:24