0

フォームで使用されている情報からいくつかのデータベース行を更新しようとしていますが、いくつかのエラーがあります

問題のコードは次のとおりです。

  var html = '';
$(document).ready(function(){
    $(".save_btn").on('click', function() {

        $('.response').each(function(){
            //alert($(this).attr('id'));

            var $no = no.checked;
            var $yes = yes.checked;

            alert($no);
            alert($yes);

            if ($no === 'no') {
                html = $.ajax({
                    url: "response14.php?questionID=" + $(this).attr('id') + "&question=" + $(this).val() + "&check=2",
                    async: false
                }).responseText;
            }   
            if ($yes === 'yes') {
                html = $.ajax({
                    //url: "response.php?questionID=" + $(this).attr('id') + "&response=" + $(this).val() + "&check=1",
                    url: "response14.php?questionID=" + $(this).attr('id') + "&question=" + escape($(this).val()) + "&check=1",
                    async: false
                }).responseText;
            }   

        }); 
        alert(html);
        location.reload();  
    });
})

response14.php:

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

ボタン付きのコード:

echo "<script src='viewsonly.js' type='text/javascript'> </script><br><center>";    
    include("db_conn.php");

$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' id='{$row['questionID']}' class='response textbox'>{$row['question']}</textarea>
    YES: <input type='checkbox' name='yes' value='yes'> &nbsp;&nbsp;&nbsp;
    NO: <input type='checkbox' name='no' value='no'>    </div></td></tr>";
            }
            echo "</tbody></table>";
            echo "<button type='button' class='save_btn' style='align:right'>Save All</button><br>";

レンダリングされたhtml:

   <td style='border:1px white; padding: 5; margin: 5;'><div id='wrap'>
<textarea cols='85' rows='2' id='3792' class='response textbox'>Hello C!!

Where can I send you fan mail? :)
I want your autograph and I'm from the Philippines :)

God bless Cooper!</textarea>
    YES: <input type='checkbox' name='yes' value='yes'> &nbsp;&nbsp;&nbsp;
    NO: <input type='checkbox' name='no' value='no'>    </div></td></tr><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' id='3793' class='response textbox'>What is your religion?
Do you believe in God?
How much you measure height?</textarea>
    YES: <input type='checkbox' name='yes' value='yes'> &nbsp;&nbsp;&nbsp;
    NO: <input type='checkbox' name='no' value='no'>    </div></td></tr></tbody></table><button type='button' class='save_btn' style='align:right'>Save All</button><br>

エラーメッセージは次のとおりです。

TypeError: $(...).live は関数ではありません

$(".save_btn").live('クリック', function() {

理由についてのアイデアはありますか?

4

2 に答える 2

4
TypeError: $(...).live is not a function

$(".save_btn").live('click', function() {

上記のエラーは、使用.live()の新しいバージョンから廃止されたためですjQuery.on()

$(".save_btn").on('click', function() {
 -------------^^^----
于 2013-09-19T08:43:18.760 に答える
1
  1. .live は非推奨です .on を使用してください

  2. no と yes はどこで定義されていますか? <input type="radio" id="no" />必要な場合$("#no")でも、値をテストすると意味がありません。

  3. HTML を表示してください - 複数の回答があり、それぞれに「はい」/「いいえ」がありますか? その場合、名前を付けて検索する必要があります。ID は一意である必要があります

Ajax も必要ありません。

$(function(){
  $(".save_btn").on('click', function() {
    var check = $("input[name=no]").is(":checked")?2:1;
    // $(this).attr("id") next is the ID of the save button
    location = "response14.php?questionID=" + $(this).attr('id') +
       "&question=" + $(this).val() + "&check="+check;
  });          
});          

本気で使う

YES: <input type='checkbox' name='yes[]' value='yes'>
NO: <input type='checkbox' name='no[]' value='no'>   

フォーム全体を送信するだけです!!!

ajaxが必要な場合(コメントから、投稿したいので必要になる可能性があることがわかります)、これを試してください

$(function(){
  $(".save_btn").on('click', function(e) {
    e.preventDefault() // in case you have a submit button
    var check = $("input[name=no]").is(":checked")?2:1;
    $.post("response14.php",{
        "questionID":$(this).attr('id'),
        "question":$(this).val(),
        "check":check
      },function(response) { 
      $("#someOutputDiv").html(response);
    });
  });          
});          
于 2013-09-19T08:53:58.457 に答える