javascript - get all selected values from ListBox (client side) - Stack Overflow

I'm trying to implement a custom validator in JavaScript for my vb page. This validator should che

I'm trying to implement a custom validator in JavaScript for my vb page. This validator should check if in a multichoice listbox there aren't any selected values, showing an error pop up if so.

The thing is, I want to do it client side, but in my 'validateFunction' function I only get the last selected (or unselected, if it was selected already) item. I know how to do it in code-behind code, but I want to do it client-side.

aspx code:

 <asp:ListBox ID="lbEdit" runat="server" SelectionMode="Multiple">
 </asp:ListBox>
 <asp:CustomValidator id="cvEdit" runat="server" Display="None" ControlToValidate="lbEdit" ClientValidationFunction="validateFunction"/>
 <ajax:ValidatorCalloutExtender runat="server" ID="vceEdit" TargetControlID="cvEdit" />

JavaScript code:

 function validateFunction(source, arguments) {
        var options = document.getElementById(source.controltovalidate).options;
            for (var i = 0; i < options.length; i++) {
                if (options[i].selected == true) {
                    args.IsValid = true;
                    return;
                }
            }
            args.IsValid = false;
 }

As I said, since last (un)selected item will be the only one selected in the 'options' array, the validating function will always return true...

I thought of populating another array in another javascript function as the 'true selected array' and pare its values with the selected option each time the function fires.. but I think there should be a better way.

So... Is there a straight forward way to get ALL the selected items from a multichoice listbox in javascript?

I'm trying to implement a custom validator in JavaScript for my vb page. This validator should check if in a multichoice listbox there aren't any selected values, showing an error pop up if so.

The thing is, I want to do it client side, but in my 'validateFunction' function I only get the last selected (or unselected, if it was selected already) item. I know how to do it in code-behind code, but I want to do it client-side.

aspx code:

 <asp:ListBox ID="lbEdit" runat="server" SelectionMode="Multiple">
 </asp:ListBox>
 <asp:CustomValidator id="cvEdit" runat="server" Display="None" ControlToValidate="lbEdit" ClientValidationFunction="validateFunction"/>
 <ajax:ValidatorCalloutExtender runat="server" ID="vceEdit" TargetControlID="cvEdit" />

JavaScript code:

 function validateFunction(source, arguments) {
        var options = document.getElementById(source.controltovalidate).options;
            for (var i = 0; i < options.length; i++) {
                if (options[i].selected == true) {
                    args.IsValid = true;
                    return;
                }
            }
            args.IsValid = false;
 }

As I said, since last (un)selected item will be the only one selected in the 'options' array, the validating function will always return true...

I thought of populating another array in another javascript function as the 'true selected array' and pare its values with the selected option each time the function fires.. but I think there should be a better way.

So... Is there a straight forward way to get ALL the selected items from a multichoice listbox in javascript?

Share Improve this question edited Sep 11, 2017 at 10:33 D Ie asked Sep 11, 2017 at 7:08 D IeD Ie 8417 silver badges23 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5 +50

Your question is a bit unclear, and your function seems working, but what I will answer here is the last part of your question So... Is there a straight forward way to get ALL the selected items from a multichoice listbox in javascript?

Try this js function

<script type="text/javascript">
        function validateFunction() {
            var options = document.getElementById('<% = this.lbEdit.ClientID %>').options;
            var selectedItems;
            for (var i = 0; i < options.length; i++) {
                if (options[i].selected == true) {
                    if (selectedItems) {
                        selectedItems = selectedItems + ";" + options[i].value;
                    }
                    else {
                        selectedItems = options[i].value;
                    }
                }
            }
            if (selectedItems) {
                alert(selectedItems);
                return true;
            }
            else {
                alert("No item was selected");
                return false;
            }
        }
    </script>

And this is the aspx code, I removed the validator

 <asp:ListBox ID="lbEdit" runat="server" SelectionMode="Multiple" >
 </asp:ListBox>
    <asp:Button ID="test" runat="server" Text="send"  OnClientClick="return validateFunction();"/>

In the developer tools of your browser you can see a detailed information about all the properties of your object

let a1 = [
{
    Selected:false,
},
{
    Selected:false
},
{
    Selected:false
}];

if (a1.some(i => i.Selected)) console.log(true); else console.log(false);

Maybe what you want

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742307726a4419275.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信