简体   繁体   English

jQuery从多选择列表中获取值,如果语句MVC3

[英]JQuery Get values from Multi Select List if statement MVC3

I am trying to iterate through the list of values populating my select box, then set up a string based on the outcome. 我试图遍历填充选择框的值列表,然后根据结果设置一个字符串。 The string should contain a 1 for each selected item, and a zero for each item that isn't selected. 对于每个所选项,字符串应包含1,对于未选中的每个项应包含零。 I had been trying to use #Select option:selected to check that, but then found out that that selects all the options that are selected, so that just gives me all 1s in my string. 我一直在尝试使用#Select选项:选中它来检查,但后来发现它选择了所有选中的选项,所以只给我字符串中的所有1。 Below is my select box: 以下是我的选择框:

<select multiple="multiple" id="Select">
    @foreach (var val in Model.SelectStuff)
    {
        <option value="@val.Key">@val.Descript</option>
    }
</select>

And the JQuery: 和JQuery:

var admaList = "";

$("#Select option").each(function () {
    if ($("#Select option:selected")) {  //I know this doesn't work!
        list += "1,";
    }
    else {
        list += "0,";
    }
});

So, 2 questions. 那么,2个问题。 One, is how can I utilize this (if its possible), and is there a list that I could use in JQuery instead of passing back a string to the control that I am going to have to break into a list later anyway? 一,是如何利用它(如果可能的话),是否有一个我可以在JQuery中使用的列表,而不是将一个字符串传递给控件,​​我将不得不在以后打入列表?

$('#Select').val() returns an array of the selected options from the multiple select element. $('#Select')。val()从多选元素中返回所选选项的数组。

http://api.jquery.com/val/ http://api.jquery.com/val/

So, in your case, I'm not sure if a string of 0's and 1's is actually what you want. 因此,就您的情况而言,我不确定您真正想要的是0还是1的字符串。 If, for example, the "value" of the options are ids then take: 例如,如果选项的“值”为id,则采用:

<select id="Select" multiple="multiple">
    <option value="1">Value 1</option>
    <option value="2">Value 2</option>
    <option value="3">Value 3</option>
</select>

If 1 and 3 are selected, then 如果选择1和3,那么

$('#Select').val() // returns [1,3]

If you want to pass that to the server, then 如果要将其传递给服务器,那么

var selectedValues = $('#Select').val() || [];
selectedValues.join(","); // return "1,3"

Have your tried not using jquery like this. 你试过不要像这样使用jquery。 It is possible to do it with jquery but a view model can also be used. 可以使用jquery来完成它,但也可以使用视图模型。 In the view you can wire a List of selected values to your listbox. 在视图中,您可以将选定值列表连接到列表框。 The user will be able to pick one or more options and it will automatically populate the selected values. 用户将能够选择一个或多个选项,它将自动填充选定的值。 Below is an example. 以下是一个例子。 Also I typed this without really compiling but i hope you get the idea. 我也没有真正编译就打了这个字,但我希望你能理解。

View 视图

@Model SelectStuff

 @using (Html.BeginForm("Index","Home",FormMethod.Post))
        {
            @Html.ValidationSummary(true)
            @Html.ListBox("SelectStuffSelected", Model.SelectStuff, new { @class = "whatever here" })
       // NOTE!!!!PAY ATTENTION HERE SelectStuffSelected will be used in the viewmodel!!!!!


                <p >
                    <input type="submit" value="Submit" />
               </p> 
    }

Controller 调节器

public ActionResult index()
return view();
[HttpPost]
public ActionResult(SelectStuff model)
{
  return view(model);
}

ViewModel 视图模型

  public class SelectStuff
    {
    public SelectStuff(){InitialSignature();}

private void InitialSignature()
        {
            List<SelectListItem> IntialSignatureRaw = new List<SelectListItem>();
            IntialSignatureRaw.Add(new SelectListItem
            {
                Text = "Yes",
                Value = "1",
                Selected = true
            });


            IntialSignatureRaw.Add(new SelectListItem
            {
                Text = "No",
                Value = "0"
            });

            SelectStuff= new SelectList(IntialSignatureRaw, "Value", "Text");
        }
//here comes the magic 
        public virtual MultiSelectList SelectStuff {get;set;}
        public virtual List<string> SelectStuffSelected{get;set;}
      //PAY ATTENTION HERE!!!!! do you see SelectStuffSelected is wired to the view above?
}

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

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