后台返回数据回显,使用js控制默认选中复选框和下拉框

2021-05-17 19:30

阅读:758

标签:self   sele   smt   select   javascrip   val   string   锤子   span   


html lang="zh-CN">
head>
meta charset="UTF-8">
title>测试title>
array(‘ch1‘,‘ch3‘,‘ch5‘); //后台获取checkbox选中id的数组
    $slt = ‘iph‘;                    //后台获取select选中option的value
?>
script type="text/javascript" src="//upcdn.b0.upaiyun.com/libs/jquery/jquery-1.8.3.min.js">script>


script type="text/javascript">
$(function(){

    var chkData = ;
    var sltData = ‘;

    $(‘.chk‘).each(function(){
        var self = $(this);
        var selfId = self.attr(‘id‘);
        $.each(chkData, function(n, v){
            if(v == selfId) self.attr(‘checked‘ , ‘true‘);
        });
    });

    $(‘#select option‘).each(function(){
        var self = $(this);
        var selfVal = self.val();
        if(selfVal == sltData) self.attr(‘selected‘ , ‘true‘);
    });

});
script>


head>
body>

    p>input id="ch1" class="chk" type="checkbox" value="111">111p>
    p>input id="ch2" class="chk" type="checkbox" value="222">222p>
    p>input id="ch3" class="chk" type="checkbox" value="333">333p>
    p>input id="ch4" class="chk" type="checkbox" value="444">444p>
    p>input id="ch5" class="chk" type="checkbox" value="555">555p>

    div class="selectAera">
        select id="select">
            option value="smt">锤子option>
            option value="iph">苹果option>
            option value="xm">小米option>
            option value="nka">诺基亚option>
        select>
    div>

body>
html>

后台返回数据回显,使用js控制默认选中复选框和下拉框

标签:self   sele   smt   select   javascrip   val   string   锤子   span   

原文地址:http://www.cnblogs.com/jackduan/p/7742092.html


评论


亲,登录后才可以留言!