Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
i have this code for adding multiple checked item to MVC5 Listbox but it is not working

JavaScript
$('#addRecipientList').click(function () {
    var chkBox = $('#chooseRecipient').val();
    var isChecked = $('#chooseRecipient').is(":checked");
    var lst = $("#recipientList");
    var options = $("#recipientList option");
    var arr = [];
    var alreadyExist = false;

    if (isChecked) {

        $(options).each(function () {
            if ($(this).val() == chkBox) {
                $("#errormsg").text('Mobile Number already exist')
                //alert("Mobile Number already exists");
                alreadyExist = true;
                uncheck();
                $('#loginForm').hide();
                $("#errormsg").fadeOut(5000);
                $('#chooseRecipient').checked = false;
                return;
            }

        });
       
        if (!alreadyExist)

            $(lst).append('<option value="' + chkBox + '">' + chkBox + '</option>');
        
        return true;
    }
});


please is there anything i am missing?

help please thank you for your time
Posted
Updated 9-Jun-14 1:02am
v2
Comments
Jeek_online 9-Jun-14 7:20am    
jsFiddle please.
Member 10765778 9-Jun-14 8:03am    
am using mvc for it

<div>
<label>Recipients </label>
@Html.ListBoxFor(model => model.SelectedRecipient, Model.Recipients, new { id = "recipientList", style = "width: 250px; height: 160px;", name = "recipientList" })
<div id="errormsg" class="errorMsg"></div>
</div>

in combination with angular for listing in the table
<tbody>
<tr ng-repeat="contact in contacts">
<td><input type="checkbox" value="{{contact.MobileNumber}}" ng-checked="contact.checked" name="chooseRecipient" id="chooseRecipient"></td>
<td>{{contact.Name}}</td>
<td>{{contact.MobileNumber}}</td>

</tr>
</tbody>

1 solution

Here is i have some sample code to do the job:

HTML :

XML
<input type="checkbox" id="chkTest1" value="Sports" />Sports <br />
        <input type="checkbox" id="chkTest2" value="Entertainment" />Entertainment <br />
        <input type="checkbox" id="chkTest3" value="Travel" />Travel <br />
        <input type="checkbox" id="chkTest4" value="Home" />Home <br />
        <input type="checkbox" id="chkTest5" value="Test" />Test <br />
        <select id="lstTest" size="5">
        </select>
        <input type="button" id="btnTest" value="Test" />


JS :

JavaScript
$(function () {
            $('#btnTest').on('click', function () {
                $.each($("input[type='checkbox']:checked"), function (i, chk) {
                    var hasValue = !!$('#lstTest > option[value="' + $(chk).val() + '"]').length;
                    if (!hasValue) $('#lstTest').append('<option value="' + $(chk).val() + '">' + $(chk).val() + '</option>');
                });
            });
        });



This example adds the checked items to the listbox only.

Hope this will give you some idea at least on what you want to accomplish.
 
Share this answer
 
Comments
Member 10765778 9-Jun-14 11:54am    
yea I tried testing it on fiddle but But it wasnt responding so i think there something missing... check it out http://jsfiddle.net/7SGFE/

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900