简体   繁体   English

ajax调用后未获得文本框值

[英]Text box value not geting after ajax call

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 the else part of the function is below: 我有一个具有两种形式的php页面。在第一种形式中,有一个按钮,按钮的onclick()调用用于验证第一种形式的函数。如果都进行了验证,则该函数的else部分如下:

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. else部分工作之后, hd_saleid具有作为OFKXM .The hd_saleid是第二种形式。

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: 在第二种形式中,有一个文件上传部分,这是通过使用ajax上载完成的。但是在该ajax上载函数中, hd_saleid值没有得到。这是代码:

$(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? 那么,如何在ajax上传中hd_saleid的值隐藏字段?

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. saleid需要它从价值$("#hd_saleid").val()当DOM准备好,所以在开始的时候出现了他的价值,这就是它。 That why you had to add $("#hd_saleid").val() directly in AJAX Call. 这就是为什么您必须直接在AJAX调用中添加$("#hd_saleid").val()

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...');
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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