简体   繁体   English

为什么preventDefault函数不能始终正常工作?

[英]Why doesn't the preventDefault function work always?

I'm trying to create a quiz with PHP and Javascript. 我正在尝试使用PHP和Javascript创建测验。

After a user clicked for an answer, an ajax request starts and the result is been given in the response. 用户单击答案后,Ajax请求开始,并且结果已在响应中给出。 If it was true, the clicked button gets a 'success' class and if false, a 'wrong' class. 如果为true,则单击的按钮将获得“成功”类别,如果为false,则将获得“错误”类别。

After this the buttons are disabled and the user can click to the next question. 此后,按钮将被禁用,用户可以单击到下一个问题。 But the problem is, that after one or two questions, the javascript function doesn't work and the default action of the form is being called. 但是问题是,经过一两个问题,javascript函数无法正常工作,并且正在调用表单的默认操作。

 $(document).ready(function(){ // Get the form. var form = $('#bilderquiz'); // Set up an event listener for the contact form. $(form).submit(function(e) { // Stop the browser from submitting the form. // Serialize the form data. var formData = $(form).serialize(); // Submit the form using AJAX. e.preventDefault(); $.ajax({ type: 'POST', url: $(form).attr('action'), data: formData , async: false }) .done(function(response) { var submittedAnswer = $("input[type=submit][clicked=true]"); var allAnswers = document.querySelectorAll( ".answerButton" ); alert(response); if(response == 'trueAnswer'){ submittedAnswer.removeClass('wrongAnswer'); submittedAnswer.addClass('trueAnswer'); $(".answerButton").attr('disabled', true); } else{ submittedAnswer.removeClass('trueAnswer'); submittedAnswer.addClass('wrongAnswer'); $(".answerButton").attr('disabled', true); } }) .fail(function(data) { // Set the message text. if (data.responseText !== '') { $(formMessages).text(data.responseText); } else { $(formMessages).text('Oops! An error occured and your message could not be sent.'); } }); }); }); function setClicked(input){ $(input).parents("form").removeAttr("clicked"); $(input).attr("clicked", "true"); } 
 <div class="answerChar">A</div> <form id="bilderquiz" action="includes/functions.php" method="post"> <button class="answerButton" type="submit" id="selection" name="selection" value="0" onclick="setClicked(this)"> <?php echo $questionData['answerButtons'][0] ;?> </button> </form> </div> <div class="col-sm-6"> <div class="answerChar">B</div> <form id="bilderquiz" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <button class="answerButton" type="submit" id="selection" name="selection" value="1" onclick="setClicked(this)"> <?php echo $questionData['answerButtons'][1];?> </button> </form> </div> </div> <div class="row"> <div class="col-sm-6"> <div class="answerChar">C</div> <form id="bilderquiz" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <button class="answerButton" type="submit" id="selection" name="selection" value="2" onclick="setClicked(this)"> <?php echo $questionData['answerButtons'][2];?> </button> </form> </div> <div class="col-sm-6"> <div class="answerChar">D</div> <form id="bilderquiz" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <button class="answerButton" type="submit" id="selection" name="selection" value="3" onclick="setClicked(this)"> <?php echo $questionData['answerButtons'][3];?> </button> </form> </div> </div> <div class="row"> <div class="col-sm-12"> <form id="bilderquiz" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post"> <button class="submitButton" type="submit" id="selection" name="selection" value="4" onclick="setClicked(this)">Nächste Frage</button> </form> </div> </div> 

My second problem is, that this lines also doesn't work: 我的第二个问题是,这行代码也无效:

 submittedAnswer.removeClass('wrongAnswer'); submittedAnswer.addClass('trueAnswer'); 

Your forms need unique IDs, right now they are all id="bilderquiz" Or you could use classes. 您的表单需要唯一的ID,现在它们都是id =“ bilderquiz”,或者您可以使用类。

The php: 的PHP:

<form class="capture-me">...</form>
<form class="capture-me">...</form>

The JS: JS:

$('form.capture-me').on('submit',function(e){ .....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM