js 当select 为某个值时,另一个input 设置成必填

需求:设备名称=终端、服务器时,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>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容