سؤال

In my site I use 10 file upload boxes. I want to get a file upload box name when I click on the box.

That means first upload box, second upload box, third upload box etc...

So if I click on the first upload box then I want to get name of that file upload box.

How can i get the upload button name in ajax function.

This is my ajax code:

$(function(){
    var countfile = 10;
    var strfileid = '';


    for(i=1;i<=countfile;i++){
    var btnUpload=$('#browse'+i);

    var adinfoid=$('#adinfoid').val();
    new AjaxUpload(btnUpload, {
        action: '<?php echo base_url()?>index.php/post/upload_editgalleryimage/'+adinfoid,
        name: 'uploadfile',
        onSubmit: function(file, ext){
        alert(btnUpload.Name);
            var photoplancnt=$('#photoplancnt').val();
            var hidcountimg=$('#hidcountimg').val();
            if(parseInt(hidcountimg)>=parseInt(photoplancnt)){
                $("#photoerror").html('maximum '+photoplancnt +' files are allowed');
                $("#photoerror").css('display','block');
                return false;
            }
            if (! (ext && /^(jpg|png|jpeg|gif|JPG|PNG|JPEG|GIF)$/.test(ext))){
                $("#photoerror").html('Only JPG, PNG, GIF, files are allowed');
                $("#photoerror").css('display','block');
                return false;
            }           
        },
        onComplete: function(file, response){ 

            if(response){                   
                    $(".upload_main_div").html('');
                    $(".upload_main_div").html(response);

                    var insid = $("#hiddengalidnow").val();
                    calltoloadimage(insid);
                    /*$("#galimageicon").attr("src",response);  
                    $("#galimageicon").attr("width",55);
                    $("#galimageicon").attr("height",55);*/
                    //$("#mainimageicon1").attr("src",response);

            }else{
            alert("error");
            }
        }
    }); 
    }
});

It will alert 'browse12' at all time.

Html code:

<?php
for($i=1;$i<=10;$i++){
?>
<input type="button" id="browse<?php echo $i;?>" name ="browse<?php echo $i;?>" class="browse_media" value="Browse">
<?php
}
?>
هل كانت مفيدة؟

المحلول

Finally

alert(this._button.name);

نصائح أخرى

Generally:

$('input[type=file]').on('click', function() {
 var name = $(this).attr('name'); // or this.name
});

for you case:

btnUpload.attr('name');

Maybe this will help

$("input[id^=browse]").click(function(){
var strBrowseName = $(this).attr('name'); 
// Or no var, if it has already been defined 
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top