2017-03-14 1 views
0

このコードを使用しようとしています。しかし、Ajaxは機能せず、Submitボタンを押すとページがリフレッシュされます。私は間違いがどこにあるのか分かりません。私はAjaxを使用していますが、サブミット・ボタンをクリックするとページがまだリフレッシュされています

複数回答を印刷する必要があるため、私はjsonを使用しています。たとえば、それぞれの答えが正しいかどうかを示す必要があります。

ヘッダーにjQueryがロードされていますが、これは問題ありません。

<script> 
$(document).ready(function() { 
    $('form').submit(function(event) { 
    var parametros = { 
      "a" : a, 
      "b" : b, 
      "c" : c, 
      "e" : d, 
      "d" : e 
    }; 
    $.ajax({ 
      data: parametros, 
      url: 'exercise_matching_code.php', 
      dataType: 'json', 
      type: 'post', 
      beforeSend: function() { 
        $("#resultado").html("Procesando, espere por favor..."); 
      }, 
      success: function (response) { 
      $(".message1").html(data.message1); 
      $(".message2").html(data.message2); 
      $(".message3").html(data.message3); 
      $(".message4").html(data.message4); 
      $(".message5").html(data.message5); 
      } 
    }); 
    }); 
}); 
</script> 
    <div class="row"> 
    <div class="large-12 columns"> 
    <p>Übung: Finde das Gegenteil:</p> 
    <table style="width:100%"> 
     <tr> 
     <td>a) schön</td> 
     <td>1 alt</td> 
     </tr> 
     <tr> 
     <td>b) groß</td> 
     <td>2 klein</td> 
     </tr> 
     <tr> 
     <td>c) neu</td> 
     <td>3 langweilig</td> 
     </tr> 
     <tr> 
     <td>d) laut</td> 
     <td>4 leise</td> 
     </tr> 
     <tr> 
     <td>e) interessant</td> 
     <td>5 hässlich</td> 
     </tr> 
    </table> 
    </div> 
    </div> 
    <form action="" method="post"> 
    <div class="row"> 
     <div class="large-12 columns"> 
     <table style="width:100%"> 
     <tr> 
     <td>a)</td> 
     <td> 
      <select name="a"> 
      <option value="1">1</option> 
      <option value="2">2</option> 
      <option value="3">3</option> 
      <option value="4">4</option> 
      <option value="5">5</option> 
      </select> 
      <div class="message1"></div> 
     </td> 
     </tr> 
     <tr> 
     <td>b)</td> 
     <td> 
      <select name="b"> 
      <option value="1">1</option> 
      <option value="2">2</option> 
      <option value="3">3</option> 
      <option value="4">4</option> 
      <option value="5">5</option> 
      </select> 
      <div class="message2"></div> 
     </td> 
     </tr> 
     <tr> 
     <td>c)</td> 
     <td> 
      <select name="c"> 
      <option value="1">1</option> 
      <option value="2">2</option> 
      <option value="3">3</option> 
      <option value="4">4</option> 
      <option value="5">5</option> 
      </select> 
      <div class="message3"></div> 
     </td> 
     </tr> 
     <tr> 
     <td>d)</td> 
     <td> 
      <select name="d"> 
      <option value="1">1</option> 
      <option value="2">2</option> 
      <option value="3">3</option> 
      <option value="4">4</option> 
      <option value="5">5</option> 
      </select> 
      <div class="message4"></div> 
     </td> 
     </tr> 
     <tr> 
     <td>e)</td> 
     <td> 
      <select name="e"> 
      <option value="1">1</option> 
      <option value="2">2</option> 
      <option value="3">3</option> 
      <option value="4">4</option> 
      <option value="5">5</option> 
      </select> 
      <div class="message5"></div> 
     </td> 
     </tr> 
     <tr> 
    </table> 
     </div> 
    </div> 
    <div class="row"> 
     <div class="large-12 columns submitting"> 
     <input type="submit" value="Go" class="submit"> 
     </div> 
     </div> 
    </form> 

そして、これは、PHPファイルです: -

<?php 
$a = $_POST["a"]; 
$b = $_POST["b"]; 
$c = $_POST["c"]; 
$d = $_POST["d"]; 
$e = $_POST["e"]; 
if ($a == '2') { 
    $answer1 = "correct answer"; 
    echo $answer1; 
} else { 
    $answer1 = "wrong answer"; 
    echo $answer1; 
} 
if ($b == '4') { 
    $answer2 = "correct answer"; 
} else { 
    $answer2 = "wrong answer"; 
} 
if ($c == '1') { 
    $answer3 = "correct answer"; 
} else { 
    $answer3 = "wrong answer"; 
} 
if ($b == '5') { 
    $answer4 = "correct answer"; 
} else { 
    $answer4 = "wrong answer"; 
} 
if ($b == '3') { 
    $answer5 = "correct answer"; 
} else { 
    $answer5 = "wrong answer"; 
} 
echo json_encode(
    array(
    "message1" => "$answer1", 
    "message2" => "$answer2", 
    "message3" => "$answer3", 
    "message4" => "$answer4", 
    "message5" => "$answer5", 
) 
) 
?> 
+0

デベロッパーコンソールを開いてエラーを確認します。 –

+2

'event.preventDefault()'それを見て、それを使ってください – RiggsFolly

+0

'$("#resultado ")。html'結果を示すためにidはどこですか? –

答えて

3

使用event.preventDefault()以下のように: -

$('form').submit(function(event) { 
    event.preventDefault(); 

    //rest of the code 

}); 

参考: - あなたは、次の作品を持っている必要がありhttps://api.jquery.com/event.preventdefault/

1

フォームが送信されないようにJavaScriptの

$('form').on({ 
    submit: function() { 
     return false; 
    } 
}); 
+0

しかし、今は何も起こりません。 –

+0

上記のコメントを使用すると、フォームが「サブミット」されたときでもコードを実行することができます。 $( 'form').submit(function(event){event.preventDefault(); //残りのコード}); –

関連する問題