How to get all ListBox items on submit action

五迷三道 提交于 2019-12-10 18:15:36

问题


How can I get all ListBox values (not just the selected items) upon submit in an asp.net MVC2 project?

I'm using Ajax forms like Ajax.BeginForm("ActionName", new...).

I have already tried to select all the items in the OnBegin event of the Ajax options but not all of the ListBox items are being POSTed to the controller.


回答1:


This code works for me!

<script type="text/javascript">
$(document).ready(function () {

    $("#myForm").submit(function (e) {

        $("#myList option").prop("selected", "selected");

    });
}); 
</script>



回答2:


This was my solution.

HTML:

<input type="submit" value="Save Changes" onmouseover="SelectAllItems()" />

JavaScript:

function SelectAllItems() {
    $("#UnlinkedProp").each(function() { 
        $("#UnlinkedProp option").attr("selected", "selected"); 
    }); 

    $("#LinkedProp").each(function() { 
        $("#LinkedProp option").attr("selected", "selected"); 
    }); 

    $("#UnlinkedProp").focus(); 

    $("#LinkedProp").focus();
}


来源:https://stackoverflow.com/questions/2599957/how-to-get-all-listbox-items-on-submit-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!