Php 单击单选按钮提交(通过ajax)并替换为();不起作用
我正在创建一个调查。如果有人单击单选按钮(特别是“评级4”或“评级5”),我希望表单通过ajax提交。如果他们单击其他任何内容,我希望表单引入div#getMore,他们将通过submit按钮提交(通过ajax/jquery) 我有两个问题:Php 单击单选按钮提交(通过ajax)并替换为();不起作用,php,jquery,ajax,Php,Jquery,Ajax,我正在创建一个调查。如果有人单击单选按钮(特别是“评级4”或“评级5”),我希望表单通过ajax提交。如果他们单击其他任何内容,我希望表单引入div#getMore,他们将通过submit按钮提交(通过ajax/jquery) 我有两个问题: ajax在提交按钮上工作,但在单击“评级4”或“评级5”时不起作用 replaceWith();无法处理这两种提交类型 这是我的jquery/ajax: $(document).ready(function() { $('.rating').cli
$(document).ready(function() {
$('.rating').click(function() {
$('.rating').removeClass('selected');
ratingClick($(this)); // see function below...
});
});
//if rating 4 or rating 5 are clicked, submit (not working), else pull in questions 1 & 2//
function ratingClick(that) {
console.log($(that).attr('for'));
if ($(that).attr('for') == 'rating4' || $(that).attr('for') == 'rating5') {
var $form = $('#questions');
$form.submit(function(){
$.post($(this).attr('connect.php'), $(this).serialize(), function(response){
},'json');
return false;
$('#form').replaceWith( "<p>Thanks for your feedback!</p>" );
});
}
else {
$('#getMore').fadeIn();
$(that).toggleClass('selected');
}
}
// When 'submit' button is clicked, submit the form, and replace with "thank you" message (not working) //
$(document).ready(function(){
var $form = $('#questions');
$form.submit(function(){
$.post($(this).attr('connect.php'), $(this).serialize(), function(response){
},'json');
return false;
$('#form').replaceWith( "<p>Thanks for your feedback!</p>" );
});
});
<div id = "form">
<form id="questions" action="connect.php" method="post">
<h2>How helpful is this article?</h2>
<div class="ratings">
<input type="radio" name="Q1" id="rating1" value="1"><label class="rating" for="rating1">Not at all helpful</label>
<input type="radio" name="Q1" id="rating2" value="2"><label class="rating" for="rating2">Not very helpful</label>
<input type="radio" name="Q1" id="rating3" value="3"><label class="rating" for="rating3">Somewhat helpful</label>
<input type="radio" name="Q1" id="rating4" value="4"><label class="rating" for="rating4">Very helpful</label>
<input type="radio" name="Q1" id="rating5" value="5"><label class="rating" for="rating5">Extremely helpful</label>
</div>
<div id="getMore">
<h2>Please tell us why you didn't find this article helpful:</h2>
<input type='checkbox' name="Q2_1" value="1">Not related to my issue<br/>
<input type='checkbox' name="Q2_2" value="1">Too complicated explanations<br/>
<input type='checkbox' name="Q2_3" value="1">Too much information<br/>
<input type='checkbox' name="Q2_4" value="1">Incorrect information<br/>
<input type='checkbox' name="Q2_5" value="1">Unclear information<br/>
<input type='checkbox' name="Q2_6" value="1">Incomplete information<br/>
<h2>Do you have any other feedback about this article?</h2>
<p><input type="text" name="Q3" /><p>
<div id = "submit"><input type='submit' value="Submit" /></div>
</div>
</form>
$(文档).ready(函数(){
$('.rating')。单击(函数(){
$('.rating').removeClass('selected');
评级单击($(此));//请参阅下面的函数。。。
});
});
//如果单击了等级4或等级5,则提交(不起作用),否则请插入问题1和2//
功能分级点击(那个){
console.log($(that.attr('for'));
if($(that).attr('for')='rating4'| |$(that).attr('for')='rating5'){
var$form=$(“#问题”);
$form.submit(函数(){
$.post($(this.attr('connect.php'),$(this.serialize(),函数(响应){
}“json”);
返回false;
$(“#表单”)。替换为(“感谢您的反馈!”;
});
}
否则{
$('getMore').fadeIn();
$(that.toggleClass('selected');
}
}
//单击“提交”按钮后,提交表单,并替换为“谢谢”消息(不起作用)//
$(文档).ready(函数(){
var$form=$(“#问题”);
$form.submit(函数(){
$.post($(this.attr('connect.php'),$(this.serialize(),函数(响应){
}“json”);
返回false;
$(“#表单”)。替换为(“感谢您的反馈!”;
});
});
这是我正在使用的表单(供参考):
$(document).ready(function() {
$('.rating').click(function() {
$('.rating').removeClass('selected');
ratingClick($(this)); // see function below...
});
});
//if rating 4 or rating 5 are clicked, submit (not working), else pull in questions 1 & 2//
function ratingClick(that) {
console.log($(that).attr('for'));
if ($(that).attr('for') == 'rating4' || $(that).attr('for') == 'rating5') {
var $form = $('#questions');
$form.submit(function(){
$.post($(this).attr('connect.php'), $(this).serialize(), function(response){
},'json');
return false;
$('#form').replaceWith( "<p>Thanks for your feedback!</p>" );
});
}
else {
$('#getMore').fadeIn();
$(that).toggleClass('selected');
}
}
// When 'submit' button is clicked, submit the form, and replace with "thank you" message (not working) //
$(document).ready(function(){
var $form = $('#questions');
$form.submit(function(){
$.post($(this).attr('connect.php'), $(this).serialize(), function(response){
},'json');
return false;
$('#form').replaceWith( "<p>Thanks for your feedback!</p>" );
});
});
<div id = "form">
<form id="questions" action="connect.php" method="post">
<h2>How helpful is this article?</h2>
<div class="ratings">
<input type="radio" name="Q1" id="rating1" value="1"><label class="rating" for="rating1">Not at all helpful</label>
<input type="radio" name="Q1" id="rating2" value="2"><label class="rating" for="rating2">Not very helpful</label>
<input type="radio" name="Q1" id="rating3" value="3"><label class="rating" for="rating3">Somewhat helpful</label>
<input type="radio" name="Q1" id="rating4" value="4"><label class="rating" for="rating4">Very helpful</label>
<input type="radio" name="Q1" id="rating5" value="5"><label class="rating" for="rating5">Extremely helpful</label>
</div>
<div id="getMore">
<h2>Please tell us why you didn't find this article helpful:</h2>
<input type='checkbox' name="Q2_1" value="1">Not related to my issue<br/>
<input type='checkbox' name="Q2_2" value="1">Too complicated explanations<br/>
<input type='checkbox' name="Q2_3" value="1">Too much information<br/>
<input type='checkbox' name="Q2_4" value="1">Incorrect information<br/>
<input type='checkbox' name="Q2_5" value="1">Unclear information<br/>
<input type='checkbox' name="Q2_6" value="1">Incomplete information<br/>
<h2>Do you have any other feedback about this article?</h2>
<p><input type="text" name="Q3" /><p>
<div id = "submit"><input type='submit' value="Submit" /></div>
</div>
</form>
这篇文章有多大帮助?
毫无帮助
不是很有帮助
有点帮助
很有帮助
非常有用
请告诉我们为什么您觉得这篇文章没有帮助:
与我的问题无关
解释太复杂了
信息太多
错误信息
信息不清楚
信息不完整
你对这篇文章还有其他反馈吗?
我被卡住了,非常感谢你的帮助
EDIT:replaceWith();正在工作,只需将其置于返回false上方即可;(感谢之前发表评论的人)replaceWith();正在工作-只需找出如何提交“评级4”或“评级5”,我无法从这里测试这一点,但我认为您弄错的是在
评级中定义提交处理程序单击而不是触发事件。尝试将$form.submit(function(){…},'json');
替换为$form.submit()
这应该会使您已经定义的处理程序启动。但是,if语句肯定有问题-如果我替换它,它不会改变任何东西。我尝试过移动$form.submit(函数)…但没有运气=/Solved:从if语句中删除ajax,使用$('input[type=radio')。单击(function(){$(this).closest(“form”).submit();而不是