需求:设备名称=终端、服务器时,IP MAC 必填
html部分:
<tr>
<th width="80">设备名称</th>
<td>
<select name="sbname" id="sbname">
<volist name="selectarr[0]" id="vo">
<option value="{$vo}">{$vo}</option>
</volist>
</select>
</td>
</tr>
<tr>
<th width="80">IP地址 </th>
<td>
<input type="text" id="ip" style="width:400px;" name="ipaddress" value="" class="input input_hd J_title_color" placeholder="请输入..." />
<span>必填项</span>
</td>
</tr>
<tr>
<th width="80">MAC地址 </th>
<td>
<input type="text" id="mac" style="width:400px;" name="macaddress" value="" class="input input_hd J_title_color" placeholder="请输入..." />
<span>必填项</span>
</td>
</tr>
JS:
<script>
//sbname1 = $('#sbname option:selected').text();
$(document).on("change","#sbname",function(){
//alert($(this).val());//获取value
sbname1 = $(this).val();
if(sbname1==='服务器' || sbname1==='终端'){
//alert(sbname1);
$('#ip').prop("required","true");//添加required属性
$('#mac').prop("required","true");
}else{
$('#ip').removeProp("required");//移除required属性
$('#mac').removeProp("required");
}
});
sbname2 = $('#sbname').val();
if(sbname2==='服务器' || sbname2==='终端'){
//alert(sbname1);
$('#ip').prop("required","true");
$('#mac').prop("required","true");
}
</script>