如何多次添加相同的JavaScript?

时间:2014-03-26 09:33:24

标签: javascript php jquery html codeigniter

我正在使用codeigniter.I需要添加相同的java脚本多个,原因是我们3个文件上传按钮上传。保存数据我使用java-scrip插件照片这是我的视图php文件

PIC1 pic1

enter image description here PIC2

按钮2(pic2)工作正常但是button1(pic1)不是因为使用了相同的JavaScript这是JavaScript.how可以

  <script src="<?php echo $this->config->item('base_url'); ?>upload_assets/jquery.uploadify.min.js" type="text/javascript"></script>
<!--carbuddy photos upload--> 
  <script>


        <?php $timestamp = time();?>
        //javascript for file upload 1  
        $(function() {
                $('#package_file_upload1').uploadify({
                                'method'   : 'post',
                'formData' : {
                            'timestamp' : '<?php echo $timestamp;?>',
                                    'token'     : '<?php echo md5('unique_salt' . $timestamp);?>',
                                        'filetype' : '7',
                                        'id'       :<?php echo $usermetadata['ProfileID'];?>,

                        },
                        'removeTimeout' : 3,
                        'progressData' : 'speed',
                        'buttonImage' : '<?php echo $this->config->item('base_url'); ?>upload_assets/browse-btn.png',
                        'swf'      : '<?php echo $this->config->item('base_url'); ?>upload_assets/uploadify.swf',
                        'uploader' : '<?php echo $this->config->item('base_url'); ?>photo/image_upload/upload',
                         'onUploadSuccess' : function(file, data, response) {
                            window.location.reload();  
                             var obj = jQuery.parseJSON(data);
             if(obj.status == 1)
             {
                var img = obj.path+obj.name;
                var photoid = obj.id;
                if(photoid > 0){
                $('#car_stream').append('<li id="photo_'+photoid+'" class="span3" ><i class="icon-remove-sign" photo="'+photoid+'" style="cursor:pointer;position: relative; float: right; top: 4px; right: 20px;"></i><a class="thumbnail fancybox-button zoomer" data-rel="fancybox-button" title="" href="'+img+'"><div style="height: 150px;overflow: hidden;"><img style="width: 220px;" src="'+img+'" alt=""></div></a></li>');
                }
             }
             else
             {
                 alert(obj.msg);
             }
                   }
                });
        });




        //set tour buddy host mode 
$('#becarbuddy').click(function(){
    var CarMode = 0;
      if ($('#becarbuddy').is(':checked')) {
          CarMode = 1;
            }
      $.ajax({
            type:"POST",
            data:{CarMode:CarMode},
            url:'<?php echo base_url();?>host/host_cont/ajax_update_host_mode', 
            success:function(response){
            }
    });
});
</script>

相同的JavaScript(唯一不同的是'filetype':'5'和#package_file_upload12在button1(pic1)的原因是通过filetype系统将识别需要保存的地方,无论是汽车照片还是个人资料照片)代码是粘贴的,我试试。但我没有正常工作如何解决这个错误,如果有方法做id JavaScript。需要快速帮助

这是HTML代码

<input id="package_file_upload1" class="form-control input-sm" placeholder="Car picture"  name="file[]" type="file" multiple="true" >

<input id="package_file_upload12" class="form-control input-sm" placeholder="profile picture"  name="file[]" type="file" multiple="true" >

1 个答案:

答案 0 :(得分:2)

在每个输入中,添加一个名为upload-btn的类:

<input id="package_file_upload1" class="form-control input-sm upload-btn" placeholder="Car picture"  name="file[]" type="file" multiple="true" >

<input id="package_file_upload12" class="form-control input-sm upload-btn" placeholder="profile picture"  name="file[]" type="file" multiple="true" >

在您的javascript中,替换为此代码:

 //each is a function that will loop through and apply all the following
 //code to the element with class upload-btn
 $('.upload-btn').each(function () {
     //we are going to determine the file type by input 'id'
     element_id = $(this).attr('id');
     var type_of_file = '';
     if (element_id == 'package_file_upload1') {
         type_of_file = '7'; //file type for car
     } else if (element_id == 'package_file_upload12') {
         type_of_file = '5'; //file type for profile
     }

     //the uploadify method
     $(this).uploadify({
         'method': 'post',
         'formData': {
             'timestamp': '<?php echo $timestamp;?>',
             'token': '<?php echo md5('
             unique_salt ' . $timestamp);?>',
             'filetype': type_of_file, //dynamic file type
             'id': <?php echo $usermetadata['ProfileID']; ?> ,

         },
        //...rest of your code follows