简体   繁体   English

如何使用动态ID验证html元素

[英]how to validate html elements with dynamic id

I need to validate an HTML element (a text field) whose id is dynamically assigned through an array. 我需要验证其id是通过数组动态分配的HTML元素(文本字段)。 I have given my HTML code below. 我在下面给出了我的HTML代码。 Please give me a direction on how to set the HTML id in jQuery? 请给我一个方向,说明如何在jQuery中设置HTML id

<div class="form-group" ng-repeat="property in entity.entityPropertyTypes">
    <!-- PROPERTY -->
    <div class="col-md-3">
        <label class="control-label">{property.propertyLabel}}</label>
    </div>

    <!-- VALUE -->
    <!-- Text field -->
    <div class="col-md-3" ng-if="property.dataTypeId === 3 ">
        <input id="{{property.propertyId}}Value" type="text" class="form-control" ng-model="property.propertyValue">
    </div>

    <!-- Date -->               
    <div class="col-md-offset-2 col-md-1" ng-if="property.dataTypeId === 4 ">
        <datepicker date-format="MM-yyyy"> 
            <input id="{{property.propertyId}}yearEnd" type="text" class="form-control" ng-model="property.propertyValue">
        </datepicker>
    </div>

You can add any property you want and then use a selector to get the element. 您可以添加所需的任何属性,然后使用选择器来获取元素。

For example, you could add a class to the input: 例如,您可以在输入中添加一个类:

 <input id="{{property.propertyId}}Value" type="text" class="form-control myclass" ng-model="property.propertyValue">                                                                       

and then select by class 然后按班级选择

 var textToCheck = $("input.myclass").val();

You can even add a personal attribute: 您甚至可以添加个人属性:

 <input item="1" id="{{property.propertyId}}Value" type="text" class="form-control" ng-model="property.propertyValue">

and then 接着

var textToCheck = $("input[item=1]").val();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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