本文通过实例代码给大家介绍了JavaScript 通过Ajax 动态加载CheckBox复选框的方法,需要的朋友参考下吧
废话不多说了,直接给大家贴代码了,具体代码如下所示:
<html>
<head></head>
<body>
<pre name="code" class="html">
<p class="controls" id="compareyear">
</p>
//动态加载年份checkbox by dongch 20170830 function GetCompareYearByStationName(name) { var str = "
"; $.ajax({ type: "GET", //默认是GET url: '@Url.Action("*********", "*****")', data: "stationName=" + name, async: false, //同步 cache: false, //不加载缓存 success: function (obj) {//后台传来的数据:"2010,2011,2012,2013,2014,2015" var yearArray = new Array(); yearArray = obj.split(","); for (var i = 0; i < yearArray.length; i++) { if (i < yearArray.length - 3) { str += "
"; } else {//后三个年份默认选中 str += "
"; } } $("#compareyear").html(str); }, error: function () { alert("对比年限加载失败"); } }); }