Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jquery从listbox转换为数组_Jquery_Asp.net Mvc 3 - Fatal编程技术网

使用jquery从listbox转换为数组

使用jquery从listbox转换为数组,jquery,asp.net-mvc-3,Jquery,Asp.net Mvc 3,我检查了其他答案,它们似乎没有解决这个问题: var values = []; $('#EnrolledParticipants :selected').each(function (i, option) { values[i] = $(option).text(); }); 这将返回一个数组,其元素数等于所选选项数 但是,我想做的是返回一个数组,其中包含列表框中的所有内容,而不仅仅是所选内容 但当我执行此代码

我检查了其他答案,它们似乎没有解决这个问题:

  var values = [];
            $('#EnrolledParticipants :selected').each(function (i, option) {
                values[i] = $(option).text();
            });
这将返回一个数组,其元素数等于所选选项数

但是,我想做的是返回一个数组,其中包含列表框中的所有内容,而不仅仅是所选内容

但当我执行此代码时:

  var values = [];
            $('#EnrolledParticipants').each(function (i, option) {
                values[i] = $(option).text();
            });
它返回一个包含一个元素的数组,列表框的每个选项都用空格分隔。知道有什么不同吗?我可以试试“:any”或类似的东西吗

有关其他信息,我的ajax调用如下所示:

$.ajax({
                url: '@Url.Action("SaveStudy")',
                type: "POST",
                contentType: 'application/json',
                data: JSON.stringify({
                    Participants: values,
                    StudyName: $("#StudyName").val(),
                    AssessmentID: $("input:radio:checked").val()
                }),
                success: function (result) {

                    $("#notify-container").notify("create", {
                        title: 'Update',
                        text: "Study was saved to database."
                    });

                },
                error: function () {
                    alert("No banks were selected.  Add at least one bank to create an assessment.");
                }
            });
public ActionResult SaveStudy(string StudyName, string AssessmentID, string[] Participants)
        {
我的控制器标题如下所示:

$.ajax({
                url: '@Url.Action("SaveStudy")',
                type: "POST",
                contentType: 'application/json',
                data: JSON.stringify({
                    Participants: values,
                    StudyName: $("#StudyName").val(),
                    AssessmentID: $("input:radio:checked").val()
                }),
                success: function (result) {

                    $("#notify-container").notify("create", {
                        title: 'Update',
                        text: "Study was saved to database."
                    });

                },
                error: function () {
                    alert("No banks were selected.  Add at least one bank to create an assessment.");
                }
            });
public ActionResult SaveStudy(string StudyName, string AssessmentID, string[] Participants)
        {
谢谢大家!

如果
#Enrolled参与者
选择
元素,请简单尝试

$('#EnrolledParticipants option').each(function (i, option) {
      values[i] = $(option).text();
});
如果
#Enrolled参与者
选择
元素,请简单尝试

$('#EnrolledParticipants option').each(function (i, option) {
      values[i] = $(option).text();
});

成功了!我知道这是一件非常简单的事情。谢谢你的快速回复!成功了!我知道这是一件非常简单的事情。谢谢你的快速回复!