Jquery show()和hide()函数不起作用
我试图使ID为的按钮仅在用户收到错误消息时显示,问题是错误消息显示时没有任何问题,但按钮没有Jquery show()和hide()函数不起作用,jquery,Jquery,我试图使ID为的按钮仅在用户收到错误消息时显示,问题是错误消息显示时没有任何问题,但按钮没有 $(document).ready(function(){ var button = $('#back'); button.hide("fast"); button.click(function(){ $('#form').show(); $("#result").hide(); button.hide(); }); $("#pgform").s
$(document).ready(function(){
var button = $('#back');
button.hide("fast");
button.click(function(){
$('#form').show();
$("#result").hide();
button.hide();
});
$("#pgform").submit(function( event ) {
event.preventDefault();
var nome = $('input[name= Nome]').val();
var cognome = $('input[name= Cognome]').val();
var background = $('#bktx').val();
var post_data = {'nome': nome,'cognome' : cognome,'background': background};
$.post('crea_pg_db.php',post_data,function(response){
if(response.type =='error'){
$("#result").addClass('failed');
var output = "<strong>"+response.text +"</strong>";
button.show();
}
else {
$("#result").addClass('success');
var output = "<strong>"+response.text+"<br /> Nome : </strong>"+ nome +"<br /> <strong>Cognome : </strong>"+cognome;
}
$('#form').hide();
$("#result").hide().html(output).slideDown();
},'json');
}); // end submit function
});//end document ready
OP找到了一个解决方案:
我找到了解决这个问题的办法
函数.htmloutput替换了结果中的所有内容,它也替换了按钮代码,这就是它不工作的原因,我已经更改了代码,现在所有内容都工作了
$(document).ready(function(){
event.preventDefault();
var status;
var nome = $('input[name= Nome]').val();
var cognome = $('input[name= Cognome]').val();
var background = $('#bktx').val();
var post_data = {'nome': nome,'cognome' : cognome,'background': background};
$.post('crea_pg_db.php',post_data,function(response){
if(response.type =='error'){
$("#result").addClass('failed');
var output = "<strong>"+response.text +"</strong> <button id='back'>Indietro</button>";
status = 1;
}else{
$("#result").addClass('success');
var output = "<strong>"+response.text+"<br /> Nome : </strong>"+ nome +"<br /> <strong>Cognome : </strong>"+cognome;
}
$('#form').hide();
$("#result").hide().html(output).slideDown();
if(status == 1){
var button = $('#back');
button.click(function(){
$('#form').show();
$("#result").hide();
button.hide();
});
}
},'json');
}); // end submit function
});//end document ready
如果你指的是html页面,我认为没有必要,因为那里的一切都很好,只是显示/隐藏不起作用,总之按钮就在这里面:既然你找到了答案,就把它作为答案发布吧!你可以回答你自己的问题。与将其与您的问题连成一行相比,它不会太令人困惑。在问题中提供答案比$result.hide.htmloutput.slideDown更令人困惑;在原始代码中,$result.hide.appendoutput.slideDown;或$result.hide.prependoutput.slideDown;将在不覆盖按钮的情况下添加消息。
$(document).ready(function(){
event.preventDefault();
var status;
var nome = $('input[name= Nome]').val();
var cognome = $('input[name= Cognome]').val();
var background = $('#bktx').val();
var post_data = {'nome': nome,'cognome' : cognome,'background': background};
$.post('crea_pg_db.php',post_data,function(response){
if(response.type =='error'){
$("#result").addClass('failed');
var output = "<strong>"+response.text +"</strong> <button id='back'>Indietro</button>";
status = 1;
}else{
$("#result").addClass('success');
var output = "<strong>"+response.text+"<br /> Nome : </strong>"+ nome +"<br /> <strong>Cognome : </strong>"+cognome;
}
$('#form').hide();
$("#result").hide().html(output).slideDown();
if(status == 1){
var button = $('#back');
button.click(function(){
$('#form').show();
$("#result").hide();
button.hide();
});
}
},'json');
}); // end submit function
});//end document ready