事件监听器在html内容不为空时执行函数

时间:2014-05-07 07:01:45

标签: javascript jquery html

当html内容不为空时,是否可以添加事件侦听器来检查并执行某些jquery函数。我的页面中有一个div内容,只有在执行click(funcion())后才会显示;即,当执行点击功能时,返回的值将显示在上述div内容中,并且将显示div 这是我之前隐藏的html内容,

<div id="tapal_doc_Form" class="tapal_form" style="width:97%">
  <legend id="legend_tapal"><h2>Upolad Tapal Documents for <label class="tapal_head" id="ret_tapal_id"></label>:</h2> </legend> 
  <div id="multifileupload">Upload</div>
</div>  

功能在这里,

$("#sendButton").click(function(){   
var ajax_data = {
    Tdist:$("#Tdist").val(),
    Tno_curr : $("#Tno_curr").val(),
    Tyear : $("#Tyear").val(),
    Trec_date : $("#Trec_date").val(),
};
$.ajax({
    url: "<?php echo site_url('tapal/insert_new_tapal'); ?>",
    type: 'POST', 
    data: ajax_data,
    success: function(result) {
        $("#tapal_entry_form").hide();
        if(result==1){
            $("#msg_doc_upload").html("Tapal entered successfully");
        }else if(result==0){
            $("#msg_doc_upload").html("Tapal could not be registered!");
        }else{
            $("#tapal_doc_Form").show();    
            $("#ret_tapal_id").empty();
            $("#ret_tapal_id").html(result);
        }
    }       
});
});

听众功能,

    var settings = {
    url: "<?php echo site_url('tapal/document_upload'); ?>",
    method: "POST",
    allowedTypes:"jpg,png,gif,doc,pdf,zip",
    formData:{tapal_id: $("#ret_tapal_id").html(), file_type:'TAPALS' },
    fileName: "myfile",
    multiple: true,
    onSuccess:function(files,data,xhr)
    {
        $("#status").html("<font color='green'>Upload is success</font>");
    },
    onError: function(files,status,errMsg)
    {       
        $("#status").html("<font color='red'>Upload is Failed</font>");
    }
    }
  $("#multiplefileuploader").uploadFile(settings);    

现在,当'click'函数将'tapal_id'返回到我的标签'ret_tapal_id'时,我没有将该值传递给侦听器函数。侦听器仍然传递null值。 所以当第一个函数将值返回到标签'ret_tapal_id'或标签不为空时,我需要执行监听器。 请帮我。提前谢谢..

1 个答案:

答案 0 :(得分:0)

这可能是因为您的ajax调用未完成,而您正在上传之前。这可能是由于ajax的异步行为。因此,您可以等待ajax完成或在您的ajax成功函数中设置html之后调用.uploadFile()

success: function(result) {
    $("#tapal_entry_form").hide();
    if(result==1){
        $("#msg_doc_upload").html("Tapal entered successfully");
    }else if(result==0){
        $("#msg_doc_upload").html("Tapal could not be registered!");
    }else{
        $("#tapal_doc_Form").show();    
        $("#ret_tapal_id").empty();
        $("#ret_tapal_id").html(result); // add your method after this.

        var settings = {
             url: "<?php echo site_url('tapal/document_upload'); ?>",
             method: "POST",
             allowedTypes:"jpg,png,gif,doc,pdf,zip",
             formData:{tapal_id: $("#ret_tapal_id").html(), file_type:'TAPALS' },
             fileName: "myfile",
             multiple: true,
             onSuccess:function(files,data,xhr)
             {
               $("#status").html("<font color='green'>Upload is success</font>");
             },
             onError: function(files,status,errMsg)
             {       
               $("#status").html("<font color='red'>Upload is Failed</font>");
             }
         }
         $("#multiplefileuploader").uploadFile(settings);  

    } // else ends
} // success ends