简体   繁体   English

Jquery .val总是为下拉列表返回相同的值

[英]Jquery .val always return same value for dropdown

The below is my code for an ajax multiple image upload 以下是我的ajax多图像上传代码

<script>
$(document).ready(function()
{

    var settings = {
        url: "upload.php",
        method: "POST",
        allowedTypes:"jpg,png",
        fileName: "myfile",
        formData: {"projectId":$('#project_name').val()},
        multiple: true,
        onSuccess:function(files,data,xhr)
        {
            $("#status").html("<font color='green'>Upload is success</font>");
        },
        afterUploadAll:function()
        {
            alert("all images uploaded!!");
        },
        onError: function(files,status,errMsg)
        {       
            $("#status").html("<font color='red'>Upload is Failed</font>");
        }
    }
    $("#mulitplefileuploader").uploadFile(settings);

});
</script>

Below is my textfield value 以下是我的文本域值

<select class="form-control" name="project_name" id="project_name">
<option value="1">Project 1</option>
<option value="2">Project 2</option>                                    
</select>

Below is my file uploader div 下面是我的文件上传器div

<div id="mulitplefileuploader">Upload</div>

The div above use a js file that can ajax upload several images at a go to a folder, its work. 上面的div使用一个js文件,它可以ajax上传几个图像去一个文件夹,它的工作。 but when i try to send data(selected dropdown value), its always at 1 despite I use jquery to initiate. 但是当我尝试发送数据(选定的下拉值)时,尽管我使用jquery启动,但它始终为1。

Could there be something wrong at the document.ready portion. 在document.ready部分可能有问题吗?

Thanks 谢谢

When I change the value of the dropdown, and use the image upload, the project id is always 1 , despite i change the value of the dropdown and try get the value with 当我更改下拉列表的值并使用图片上传时,项目ID始终为1 ,尽管我更改了下拉列表的值并尝试获取值

$('#project_name').val()

How do I change my code so when the script to upload image is execute, it will get the current selected dropdown by the id project_name, selected.val 如何更改我的代码,以便在执行上载图像的脚本时,它将通过id project_name,selected.val获取当前选定的下拉列表

Thanks! 谢谢!

Use 采用

$('#project_name option:selected').val()

which gets the selected option's value 获取所选选项的值

Now if you want to get option text ie Project 1, Project 2 etc., you need to write 现在,如果您想获得选项文本,即项目1,项目2等,您需要编写

$('#project_name option:selected').text()

UPDATE UPDATE

The problem is actually with your code set-up. 问题实际上是您的代码设置。 You have created your settings inside $(document).ready . 您已在$(document).ready创建了settings So even though if you change selected option it will not change value inside settings defined during document ready . 因此,即使您更改了selected option它也不会更改document ready期间定义的settings值。 You need to update your settings onchange or in some other way. 您需要更新您的设置onchange或以其它方式。

Bind the upload to the submit of the form 将上传绑定到表单的提交

$(document).ready(function()
{
$('form').submit(function(e){
e.preventDefault()
    var settings = {
        url: "upload.php",
        method: "POST",
        allowedTypes:"jpg,png",
        fileName: "myfile",
        formData: {"projectId":$('#project_name option:selected').val()},
        multiple: true,
        onSuccess:function(files,data,xhr)
        {
            $("#status").html("<font color='green'>Upload is success</font>");
        },
        afterUploadAll:function()
        {
            alert("all images uploaded!!");
        },
        onError: function(files,status,errMsg)
        {       
            $("#status").html("<font color='red'>Upload is Failed</font>");
        }
    }
    $("#mulitplefileuploader").uploadFile(settings);

});
});

try $("#project_name option:selected").val();

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

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