2017-02-24 118 views
1

我想禁用一个输入,当有关于keyup函数的数据库结果时。 addClass和removeClass不起作用。他们根本不开火。任何帮助赞赏。jQuery addClass和removeClass不能在keyup ajax调用中工作

$("#patient_code").keyup(function() { 
 
    var patient_code = $(this).val(); 
 
    var patient_category = $('#patient_category').val(); 
 
    var medicine_id = $('#medicine').val(); 
 

 
    var formData = { 
 
    _token: $('[name="csrf_token"]').attr('content'), 
 
    patient_code: patient_code, 
 
    medicine_id: medicine_id, 
 
    patient_category: patient_category 
 
    } 
 

 
    $.ajax({ 
 
    type: "POST", 
 
    url: "/medicines/confirmation/checkConfirmation", 
 
    data: formData, 
 
    dataType: "JSON", 
 
    success: function(response) { 
 
     if (!jQuery.isEmptyObject(response.confirmation_status)) { 
 
     $(".patient_has_confirmationform").show(); 
 
     $(".selected_patient_code").html(response.confirmation_status[0].patient_code); 
 

 
     //THIS IS NOT FIRING      
 
     $(".btnSaveConfirmation").removeClass().addClass("btn btn-borders btn-danger btn-lg disabled").attr("disabled", true); 
 

 
     console.log("there is a result for that patient code "); 
 
     } else { 
 
     $(".patient_has_confirmationform").hide(); 
 

 
     //THIS IS NOT FIRING           
 
     $(".btnSaveConfirmation").removeClass().addClass("btn btn-borders btn-success btn-lg").attr("disabled", false); 
 

 
     console.log("there are no results for that patient code "); 
 
     } 
 
    }, 
 
    error: function(jqXHR, textStatus, errorThrown, data) { 
 
     console.log(jqXHR, textStatus, errorThrown, data); 
 
    } 
 
    }); 
 
});

+0

你确定AJAX请求成功完成?这些类是否实际应用于DOM?他们的规则是否足够具体? –

+0

@RoryMcCrossan是的。如果有结果,它们会返回一个数组,如果没有则返回空。该页面上只有一个具有该类的输入。而在ajax调用中,这些类根本不会追加。但如果我从其他的删除removeClass,它的工作原理和禁用按钮 – raqulka

+0

尝试将其分离到不同的线路,并检查确切的问题在哪里 – Kostis

回答

1

你为什么想到removeClass/addClass工作?

  1. 饲料东西.removeClass('Someclass')
  2. 如果要删除所选择它.removeClass('btnSaveConfirmation')同一类,怎么会认识到添加类的元素?
  3. 那么你的方法应该是这样this--

    $(".btnSaveConfirmation").addClass('NewClass'); 
    $('.NewClass').removeClass(".btnSaveConfirmation").addClass("btn btn-borders btn-success btn-lg").attr("disabled", false); 
    
+1

谢谢。得到它了。将在3分钟内接受 – raqulka

+1

谢谢,你给了我一个新的东西知道......:D –

1

是它的工作,如果你做到这一点?我不确定是否jquery在removeClass正确后处理addClass

$(".btnSaveConfirmation").removeClass(); 
$(".btnSaveConfirmation").addClass("btn btn-borders btn-success btn-lg"); 
$(".btnSaveConfirmation").attr("disabled", false); 

编辑: addClassattr不工作,因为你删除.btnSaveConfirmation类..

+0

我其实自己尝试过,并没有奏效。 – raqulka

+0

如果您对自己的回应有所了解,请回答。 –

1

您移除按钮类,与removeClass(btnSaveConfirmation),你应该删除使用特定的类,像$('...').removeClass('btn btn-lg btn-disabled disabled ...');

所以这意味着,你触发addClass removeClass一次,然后它找不到按钮。

解决此问题的最佳方法是使用id来识别,而不是类,然后您可以简单地从您的按钮中删除所有类,它将工作。

$("#patient_code").keyup(function() { 
 
    console.log("I do trigger on keyup!"); 
 
    
 
    if ($(".btnSaveConfirmation")[0]) $(".btnSaveConfirmation").removeClass().addClass("btn btn-borders btn-danger btn-lg disabled").attr("disabled", true); 
 
    else console.log("There's no button with class: btnSaveConfirmation"); 
 
});
#patient_code { width: 200px; border-radius: 4px; border: 1px solid #444; padding: 0px 5px; height: 25px; line-height: 25px; outline: none; } 
 

 
.btn { 
 
border: 1px solid green; 
 
} 
 
.btn-danger { background: red; } 
 
.btn-lg { height: 200px; } 
 
.disabled { opacity: 0.5; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="text" id="patient_code" value="" placeholder="Type in something" /> 
 

 
<button class="btnSaveConfirmation">Button class changes</button>