0

这是复选框:

$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>

它应该在勾选 yes 复选框时显示 1,当勾选 no 复选框时显示 2,但它始终显示 1 - 现在已修复

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

4

3 回答 3

1

您的选择器正在寻找具有no属性的输入。对于你的name属性,你必须使用这个:

var check = $("input[name='no[]']").is(":checked") ? 2 : 1;
于 2013-09-19T21:05:34.177 回答
1

我认为您需要更改语法:

var check = $("input[name='no[]']").is(":checked")?2:1;
于 2013-09-19T21:05:45.053 回答
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);
});
于 2013-09-19T21:10:36.800 回答