简体   繁体   中英

Add an asterisk to the required fields using jQuery

I have a form field as shown below and others like that too.

<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>

The Org variable is as follows:

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':{
                },
        }
  };

I want to add an asterisk to the fields in which the optional field of Org is "false" but not using ng-if.

Please help me out.Thanks

Why you want to add it via Jquery ? You can do this using CSS also.

 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM