使用 Jquery 更新 Ng 模式
Update the Ng-Pattern using Jquery
有人可以帮助我如何使用 Jquery
更新 Ng 模式
我已经完成了以下问题,但没有帮助我(大部分问题都与使用 Angular JS 更新为动态值有关)
- Question 1
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>
现在使用 Jquery 如何将 ng-pattern 值更新为 ng-pattern="/^[0-9]+(\.[5]{1})?$/"
在jQuery中,您可以使用以下方式获取特定元素:
var input = $('input[name="Hour"]');
然后设置ng-pattern
属性的值,使用:
input.attr('ng-pattern', '/^[0-9]+(\.[5]{1})?$/');
工作示例:
$(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>
或者,在 vanilla javascript 中,您可以使用以下方法获取特定元素:
var input = document.querySelector('input[name="Hour"]');
然后设置ng-pattern
属性的值,使用:
input.setAttribute('ng-pattern', '/^[0-9]+(\.[5]{1})?$/');
工作示例:
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>
有人可以帮助我如何使用 Jquery
更新 Ng 模式我已经完成了以下问题,但没有帮助我(大部分问题都与使用 Angular JS 更新为动态值有关)
- Question 1
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>
现在使用 Jquery 如何将 ng-pattern 值更新为 ng-pattern="/^[0-9]+(\.[5]{1})?$/"
在jQuery中,您可以使用以下方式获取特定元素:
var input = $('input[name="Hour"]');
然后设置ng-pattern
属性的值,使用:
input.attr('ng-pattern', '/^[0-9]+(\.[5]{1})?$/');
工作示例:
$(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>
或者,在 vanilla javascript 中,您可以使用以下方法获取特定元素:
var input = document.querySelector('input[name="Hour"]');
然后设置ng-pattern
属性的值,使用:
input.setAttribute('ng-pattern', '/^[0-9]+(\.[5]{1})?$/');
工作示例:
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>