当前位置:主页 > 网页前端 > JavaScript代码 >

JavaScript 通过Ajax 动态加载CheckBox复选框

时间:2021-08-16 09:21:52 | 栏目:JavaScript代码 | 点击:

废话不多说了,直接给大家贴代码了,具体代码如下所示:

<pre name="code" class="html">
<div class="controls" id="compareyear"> 
</div></pre><br> 
<img src="http://img.blog.csdn.net/20170830165326131?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaGFuMTQ1NTQxODE3MQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt=""><br> 
<br> 
//动态加载年份checkbox by dongch 20170830 function GetCompareYearByStationName(name) { var str = " <label class='control-label'>选择对比年限:</label>"; $.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 += "<label><input type='checkbox' 
 name='checkbox' value='" + yearArray[i] + "' />" + yearArray[i] + "  </label>"; } else {//后三个年份默认选中 str += "<label><input type='checkbox' name='checkbox' value='" + yearArray[i] + "' checked='checked' />" + yearArray[i] + "  </label>"; } } $("#compareyear").html(str); 
 }, error: function () { alert("对比年限加载失败"); } }); } 
<pre></pre> 
<pre></pre> 

总结

您可能感兴趣的文章:

相关文章