Text box value not geting after ajax call

安稳与你 提交于 2019-12-12 03:48:41

问题


I have a php page with two forms.In first form there have a button ,onclick() of the button call a function for validating the first form.if validations are all did theelse part of the function is below:

else
            {
                var cnt=$("#frmdocontact").serialize();
                $.ajax({  
                type: "POST",  
                url: "doitcontact.php",
                data: cnt,
                success: function(msg){
                var spt=msg.split('#$@$');
                $('#hid_uid').val(spt[0]);
                $('#hid_add_vehicle').val(spt[0]);
                $('#hid_saleid').val(spt[1]);

                //alert(msg);
            //console.log(contactSent);
            var pkg = $('input:radio[name=pkg]:checked').val();

            var $tabs = $('#tabs').tabs();
            var selected = $tabs.tabs('option', 'selected');
            $tabs.tabs('select', selected+1);
                //window.location="thankyou.php"
                }
                });

            }

after the else part worked ,the hd_saleid have value as OFKXM.The hd_saleid is in second form.

In second form there have file upload section ,that is done by using ajax upload.But in that ajax upload function the hd_saleid value is not getting.This is the code:

$(function(){
        var cntUp = 0;
        var btnUpload=$('#upload');
        var status=$('#status');
        var saleid = $("#hd_saleid").val();
        new AjaxUpload(btnUpload, {
            action: 'upload-file.php',
            data: {saleid: $("#hd_saleid").val()},
            name: 'uploadfile',
            onSubmit: function(file, ext){
                 if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
                    // extension is not allowed 
                    alert('Only JPG, PNG or GIF files are allowed');
                    return false;
                }
                status.text('Uploading...');
            },
            onComplete: function(file, response){
                //On completion clear the status
                status.text('image uploaded');
                cntUp++;
                //console.log(cntUp);
                //Add uploaded file to list
                if (response.toLowerCase().indexOf("success") >= 0 ) {
                //alert('success');
                //$('<li></li>').appendTo('#files').html('<img src="uploads/'+file+'" alt="" /><br />'+file).addClass('success');

                if(saleid){
                        $('<li></li>').appendTo('#files').html('<img src="uploads/'+saleid+'/'+file+'" alt="" /><br />'+file).addClass('success');
                    }else{
                        $('<li></li>').appendTo('#files').html('<img src="uploads/'+file+'" alt="" /><br />'+file).addClass('success');
                    }

                }  else{
                    $('<li></li>').appendTo('#files').text(file).addClass('error');
                    //alert('error');
                }
            }
        });

    });

So how can i get the value hidden field of hd_saleid in ajax upload?


回答1:


Yes, your problem is here:

var saleid = $("#hd_saleid").val();

saleid takes it's value from $("#hd_saleid").val() when the DOM is ready, so it takes his value at start and that's it. That why you had to add $("#hd_saleid").val() directly in AJAX Call.

And you could use:

onSubmit: function(file, ext){
    if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){ 
        // extension is not allowed 
        alert('Only JPG, PNG or GIF files are allowed');
        return false;
    }
    this.setData({
        'saleid': $("#hd_saleid").val()
    });
    status.text('Uploading...');
},


来源:https://stackoverflow.com/questions/12048164/text-box-value-not-geting-after-ajax-call

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!