select下拉框内容回显 并将对应的option设置为selected选中状态

来源:互联网 发布:香港银行开户 知乎 编辑:程序博客网 时间:2024/05/21 06:48
<script type="text/javascript">    var productId = '${product.productId}';    _findSelectedData();    function _findSelectedData(){        jQuery.ajax({            dataType: "json",            url: "/product/getProductById",            data: {productId:productId},            type: "POST",            success: function (result) {                if (result.code == 1) {                  var data = result.data;                  var productType = data.productType;//经验大类                    var isAProduct = data.isAProduct;//是否短期活动                    //方法1                   $('#productType').find('option[value='+productType+']').atrr('selected','selected');                   $('#isAProduct').find('option[value='+isAProduct+']').atrr('selected','selected');                    //方法2                    var optionsLength = document.getElementById('productType').options.length;                    var pt = document.getElementById('productType');                    for(var i = 0; i < optionsLength; i++){                        var option = parseInt(document.getElementById('productType').options[i].value);                        if(option == productType){                            pt.options[i].setAttribute("selected","true");                        }                    }                    var aLength = document.getElementById('isAProduct').options.length;                    var ap = document.getElementById('isAProduct');                    for(var j = 0; j < aLength; j++){                        var optionA = parseInt(document.getElementById('isAProduct').options[j].value);                        if(optionA == isAProduct){                            ap.options[j].setAttribute("selected","true");                        }                    }                }            }        });    }
阅读全文
0 0