简体   繁体   中英

Multiple selector using jQuery

i am using a jquery plugin and my code look somthing like this.

<script type="text/javascript">
$(document).ready(function() {
    $('#fileUpload').uploadify({ 
      'uploader': 'img/uploadify.swf',
      'script': 'uploadify.php',
      'folder': 'upload',
      'auto' : 'true',
      'cancelImg': 'img/cancel.png',
      'fileDesc': 'jpg/jpeg',
      'displayData': 'percentage',
      'fileExt': "*.jpg;*.jpeg",
      'sizeLimit' : '8388608',
      'fileDataName' : 'file',
      onComplete: function(event, queueID, fileObj, reposnse, data) 
      {
     $('#filesUploaded').append('<a href='+fileObj.filePath+'>'+fileObj.name+'</a><br>');
     $("#firstUpload").remove();

        }
      }); }); 

$(document).ready(function() {
    $('#fileUpload2').uploadify({ 
      'uploader': 'img/uploadify.swf',
      'script': 'uploadify.php',
      'folder': 'upload',
      'auto' : 'true',
      'cancelImg': 'img/cancel.png',
      'fileDesc': 'jpg/jpeg',
      'displayData': 'percentage',
      'fileExt': "*.jpg;*.jpeg",
      'sizeLimit' : '8388608',
      'fileDataName' : 'file',
      onComplete: function(event, queueID, fileObj, reposnse, data) 
      {
     $('#filesUploaded').append('<a href='+fileObj.filePath+'>'+fileObj.name+'</a><br>');
     $("#firstUpload").remove();

     }
      }); }); 
      </script>

did you notice that i am using the exact same function and i am just changing the div name, ain't that ridiculous ? now i want my jquery function to accept two div parameters. can i do that?

thank you..

Combine the two selectors, just like in CSS:

$(document).ready(function() {
    $('#fileUpload, #fileUpload2').uploadify({ 
        ...
    });
});
<script type="text/javascript">
$(document).ready(function() {
    $('#fileUpload,#fileUpload2').uploadify({ 
      'uploader': 'img/uploadify.swf',
      'script': 'uploadify.php',
      'folder': 'upload',
      'auto' : 'true',
      'cancelImg': 'img/cancel.png',
          'fileDesc': 'jpg/jpeg',
      'displayData': 'percentage',
      'fileExt': "*.jpg;*.jpeg",
      'sizeLimit' : '8388608',
      'fileDataName' : 'file',
      onComplete: function(event, queueID, fileObj, reposnse, data) {
        $('#filesUploaded').append('<a href='+fileObj.filePath+'>'+fileObj.name+'</a><br>');
        $("#firstUpload").remove();
      }
    }); }); 
</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM