[英]Update the Ng-Pattern using Jquery
Can someone help me how to update the Ng-Pattern using Jquery 有人可以帮我如何使用Jquery更新Ng模式
I have gone through below question but didn't help me(Most of the Question are related to updating to dynamic values using Angular JS) 我经历了下面的问题,但没有帮助我(大多数问题与使用Angular JS更新为动态值有关)
HTML HTML
<div class="col-md-1" style="width: 50px;" id="hourmain">
<span ng-show="addItem.Hour.$error.pattern">The number must be between 0-4</span>
<input class="form-control" type="text" name="Hour" ng-model="form.Hour" placeholder="Hour" required="required" ng-pattern="/^[0-4]+(\.[5]{1})?$/" step="0.5" min="0" max="3" maxlength="3" id="hour">
</div>
Now using Jquery How Can I update the ng-pattern value to ng-pattern="/^[0-9]+(\\.[5]{1})?$/"
现在使用Jquery如何将ng-pattern值更新为
ng-pattern="/^[0-9]+(\\.[5]{1})?$/"
In jQuery , you can grab the specific element using: 在jQuery中 ,您可以使用以下方法获取特定元素:
var input = $('input[name="Hour"]');
and then set the value of the ng-pattern
attribute, using: 然后使用以下命令设置
ng-pattern
属性的值:
input.attr('ng-pattern', '/^[0-9]+(\.[5]{1})?$/');
Working Example: 工作示例:
$(document).ready(function(){ var input = $('input[name="Hour"]'); input.attr('ng-pattern', '/^[0-9]+(\\.[5]{1})?$/'); console.log('input ng-pattern is now: ' + input.attr('ng-pattern')); });
<div class="col-md-1" style="width: 50px;" id="hourmain"> <span ng-show="addItem.Hour.$error.pattern">The number must be between 0-4</span> <input class="form-control" type="text" name="Hour" ng-model="form.Hour" placeholder="Hour" required="required" ng-pattern="/^[0-4]+(\\.[5]{1})?$/" step="0.5" min="0" max="3" maxlength="3" id="hour" /> </div> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Alternatively, in vanilla javascript , you can grab the specific element using: 另外,在普通javascript中 ,您可以使用以下方法获取特定元素:
var input = document.querySelector('input[name="Hour"]');
and then set the value of the ng-pattern
attribute, using: 然后使用以下命令设置
ng-pattern
属性的值:
input.setAttribute('ng-pattern', '/^[0-9]+(\.[5]{1})?$/');
Working Example: 工作示例:
var input = document.querySelector('input[name="Hour"]'); input.setAttribute('ng-pattern', '/^[0-9]+(\\.[5]{1})?$/'); console.log('input ng-pattern is now: ' + input.getAttribute('ng-pattern'));
<div class="col-md-1" style="width: 50px;" id="hourmain"> <span ng-show="addItem.Hour.$error.pattern">The number must be between 0-4</span> <input class="form-control" type="text" name="Hour" ng-model="form.Hour" placeholder="Hour" required="required" ng-pattern="/^[0-4]+(\\.[5]{1})?$/" step="0.5" min="0" max="3" maxlength="3" id="hour" /> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.