1.引入
<script src="/src/ejs_production.js"></script>
2.模板
<script id="content" type="text/template">
<div class="form-group col-lg-12">
<label class="control-label col-lg-3 text-right">
[% if ( type == 1 ) { %]
规格一内容:
[% } else { %]
规格二内容:
[% } %]
</label>
<span class="input-group col-lg-8 input-group-option">
<select name="[%= name %]" id="[% id %]" class="form-control spec_cont" style="position: relative; left:-4px;z-index: 1;border-radius:3px;" aria-describedby="object" multiple="multiple">
<option value="0">请选择</option>
[% for(var i=0; i<attr_val.length; i++) { %]
<option value="[%= attr_val[i].id %]" >[%= attr_val[i].name %]</option>
[% } %]
</select>
</span>
</div>
</script>
3.引入数据
$("#spec1").on("change",function(){
var data = {};
data.id = \'spec1_cont\';
data.name = \'spec1_cont\';
data.type = 1;
let attr_key_id = $(this).val();
// 获取规格内容
$.ajax({
type:\'POST\',
url:\'get_attr_val\',
data: {attr_key_id: attr_key_id},
dataType:\'json\',
success:function(res){
if(res.errno == 0){
data.attr_val = res.attr_val;
var h = new EJS({element:\'content\'}).render(data);
$(\'#spec1_div_container\').html(h);
$(\'.spec_cont\').select2();
} else {
$(\'#spec1_div_container\').html(\'\');
}
}
});
});
里面有if,有for,有=。基本就够用了!
下载ejs_production