HTML page Select drop-down list box in another way, a method once and for all.

Source: Internet
Author: User

Many times, we write the from table on one side and assign values to the select tag in this way:
Create a public partial view in 1.MVC;
2. Directly in the foreground, insert the snippet background code;
Today I use the custom attribute in HTML5, combined with the JQ extension method, write a general JS method,
The JS code is as follows:

  $.fn.            Setselect = function () {return This.each (function () {var select = $ (this);            var url = $ (this). attr ("Data-ajax");             var value = $ (this). attr ("data-selected");            Select.empty ();                    if (typeof url!== typeof undefined && typeof value!== typeof undefined) {$.ajax ({                    Type: "POST", Url:url, Async:false, DataType: "JSON",                            Success:function (Result) {for (var i = 0; i < result.length; i++) {                        Select.append ("<option value=" + Result[i].value + ">" + result[i].name + "</option>");                    } select.val (value);            },                });    }        }); };

On the front page:
<select class= "Form-control" name= "MenuID"
Data-ajax= "@Url. Action (" Getmenuinfo ", new {ModuleID = Model.moduleid})"
Data-selected= "@Model. MenuID" required></select>

                                    <script>

Initialization
$ ("Select[name= ' MenuID ')". Setselect ();
</script>

                                    好了,到此结束,我们前台代码是不是很少,所有的select都可以调用,只是传不同的数据源URL即可

HTML page Select drop-down list box in another way, a method once and for all.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.