2016-09-14 102 views
-3

我有一個如下所示的表單字段以及其他類似的表單字段。使用jQuery爲必填字段添加星號

<div class="form-group" ng-show="!role.vacancies"> 
    <label for="vacancies" class="col-md-3 control-label"> VACANCIES </label> 
    <div class="col-md-9"> 
    <input name="vacancies" type="text" id="vacancies" ng-model="Org.vacancies" placeholder="VACANCIES" class="form-control"> 
    </div> 
</div> 

的組織變量如下:

var Org = { 
     validate : { 
       'planCode':{ 
           'optional' : 'false', 
           'validation' : 'required', 
       }, 
       'organizationCode':{ 
           'optional' : 'false', 
           'validation' : 'required', 
       }, 
       'vacancies':{ 
           'optional' : 'false', 
           'validation' : 'number required', 
           'allowing' : 'range[1;100]', 
       }, 
       'planDate':{ 
           'optional' : 'false', 
           'validation' : 'required date', 
           'format' : 'MM/dd/yyyy', 
       }, 
       'priorityCode':{ 
           'optional' : 'false', 
           'validation' : 'required', 
       }, 
       'description':{ 
       }, 
       'statusCode':{ 
       }, 
     } 
    }; 

我想添加一個星號在其組織的可選字段是「假」,而不是用NG-如果字段。

請幫我一下。謝謝

+1

爲什麼你不希望使用'NG-if'? – Soviut

+0

使用像{{Ord.validate.vacancies.optional ==='false'? '*':''}}在您的模板中。 –

+0

你有CSS類來顯示星號嗎? –

回答

0

爲什麼要通過jquery添加它?你也可以使用CSS來做到這一點。

input:required { 
 
    position: relative; 
 
} 
 
input:required { 
 
    vertical-align: top; 
 
    background: url('https://cdn3.iconfinder.com/data/icons/fatcow/16/asterisk_orange.png') no-repeat center right; 
 
    background-size: 16px 16px; 
 
    padding-right: 20px; /* so text doesn't overlap with asterisk mark image */ 
 
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" /> 
 
<div class="form-group" ng-show="!role.vacancies"> 
 
    <label for="vacancies" class="col-md-3 control-label">VACANCIES</label> 
 
    <div class="col-md-9"> 
 
    <input name="vacancies" type="text" id="vacancies" ng-model="Org.vacancies" placeholder="VACANCIES with required" class="form-control" required> 
 
    </div> 
 
</div> 
 
<div class="form-group" ng-show="!role.vacancies"> 
 
    <label for="vacancies" class="col-md-3 control-label">VACANCIES</label> 
 
    <div class="col-md-9"> 
 
    <input name="vacancies" type="text" id="vacancies-1" ng-model="Org.vacancies" placeholder="VACANCIES not required" class="form-control"> 
 
    </div> 
 
</div>