我们如何在事件jquery中绑定事件?

时间:2018-12-20 07:26:08

标签: javascript jquery

<script>
    $(document).ready(function(){
        $(".submit_modal1").click(function(e){
            e.preventDefault();
            id = this.id;
            name = $("#fname_"+id).val();
            email = $("#email_"+id).val();
            phone = $("#phone_"+id).val();
            upload_resume = $("#upload_resume_"+id).val();
            $.ajax({
                type:"POST",
                data:{"id":id,"name":name,"email":email,"phone":phone,"upload_resume":upload_resume},
                url:"<?php echo base_url(); ?>send",
                success:function(data){
                    $("#send_hr").html(data);
                }
            });

            $("#upload_resume_"+id).change(function(){
                var file_data = $("#upload_resume_"+id).prop('files')[0];   
                var form_data = new FormData();                  
                form_data.append('file', file_data);
                $('#imgsss').html("<img src='<?php echo base_url(); ?>resource/loading.gif' />");
                $.ajax({
                    url: '<?php echo base_url(); ?>upload_resume',
                    dataType: 'text',
                    cache: false,
                    contentType: false,
                    processData: false,
                    data: form_data,                         
                    type: 'post',
                    success: function(php_script_response){
                        $("#imgsss").html(php_script_response);
                    }
                });
            });
        });
    });
</script>

在此代码中,我触发了两个事件,即click and change,我想在$("#upload_resume_"+id).change(function(){});中运行$(".submit_modal1").click(function(e){})。是否可以执行此操作,或者是否有其他方法可以同时触发这两个事件。我该怎么办?请帮助我。

谢谢

1 个答案:

答案 0 :(得分:0)

$("#upload_resume_"+id).change(function(){不会触发事件

如果您不能仅将PHP与其他PHP链接在一起,则可能是

$(function() {
  $(".submit_modal1").click(function(e) {
    e.preventDefault();
    id = this.id;
    name = $("#fname_" + id).val();
    email = $("#email_" + id).val();
    phone = $("#phone_" + id).val();
    upload_resume = $("#upload_resume_" + id).val();
    $.ajax({
      type: "POST",
      data: {
        "id": id,
        "name": name,
        "email": email,
        "phone": phone,
        "upload_resume": upload_resume
      },
      url: "<?php echo base_url(); ?>send",
      success: function(data) {
        $("#send_hr").html(data);
        $("#upload_resume_" + id).change() // HERE 
      }
    });
    $("#upload_resume_" + id).change(); // OR HERE

  });

  // any change to upload_resume*
  $("[id^=upload_resume]").change(function() {
    var file_data = $(this).prop('files')[0];
    var form_data = new FormData();
    form_data.append('file', file_data);
    $('#imgsss').html("<img src='<?php echo base_url(); ?>resource/loading.gif' />");
    $.ajax({
      url: '<?php echo base_url(); ?>upload_resume',
      dataType: 'text',
      cache: false,
      contentType: false,
      processData: false,
      data: form_data,
      type: 'post',
      success: function(php_script_response) {
        $("#imgsss").html(php_script_response);
      }
    });
  });

});