作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在表单选择选项上显示不同的输入字段。但输入验证适用于表单提交时的两个选择选项输入字段。例如:如果我选择在线选项并单击提交,它还会验证不可见的里程输入。这里分享代码。您能帮助我解决可能出现的问题吗?
<form class="form" name="form" role="form" ng-submit="locationsData()" novalidate>
<div ng-repeat="address in locationsAddress.address">
<select name="locationType" id="locationType" ng-model="locationsAddress.address[$index].type" ng-init="locationsAddress.address[$index].type = 'ONLINE'" class="form-control">
<option value="ONLINE">Online</option>
<option value="WORKER">Worker Address</option>
</select>
<div ng-if="locationsAddress.address[$index].type == 'WORKER'">
<div style="margin: 20px 0;">
<label for="miles">miles you drive?</label>
<input type="text" name="distance{{address.id}}" id="distance" placeholder="miles you can drive?" ng-model="locationsAddress.address[$index].distance" required/>
</div>
<div ng-show="form.$submitted || form.distance{{address.id}}.$touched">
<span class="error" ng-show="form.distance{{address.id}}.$error.required">Miles you drive is required.</span>
</div>
</div>
<div ng-if="locationsAddress.address[$index].type == 'ONLINE'">
<select name="chatType{{address.id}}" ng-model="locationsAddress.address[$index].appName" required>
<option value="" selected="selected">App Type</option>
<option value="SKYPE">Skype</option>
<option value="WHATSAPP">WhatsAPP</option>
</select>
<div ng-show="form.$submitted || form.chatType{{address.id}}.$touched">
<span class="error" ng-show="form.chatType{{address.id}}.$error.required">Chat required.</span>
</div>
</div>
</div>
<button type="submit" class="button">Save</button>
</form>
Angular 代码:
$scope.locationsAddress = {
};
$scope.locationsAddress.address = [
{
'id': 1,
'type': '',
'distance' : '',
'appName': ''
}
];
});
在选择选项上重置表单字段是否有帮助?如果是,请分享引用。
最佳答案
在 ng-repeat 下使用 ng-form 来分隔表单。
<form class="form" name="form" role="form" ng-submit="locationsData()" novalidate>
<div ng-repeat="address in locationsAddress.address">
<ng-form name="locationForm">
<select name="locationType" id="locationType" ng-model="address.type" ng-init="address.type = 'ONLINE'" class="form-control" ng-change="form.$setPristine();">
<option value="ONLINE">Online</option>
<option value="WORKER">Worker Address</option>
</select>
<div ng-if="address.type == 'WORKER'">
<div style="margin: 20px 0;">
<label for="miles">miles you drive?</label>
<input type="text" name="distance" id="distance" placeholder="miles you can drive?" ng-model="address.distance" required/>
</div>
<div ng-show="form.$submitted || locationForm.distance.$touched">
<span class="error" ng-show="locationForm.distance.$error.required">Miles you drive is required.</span>
</div>
</div>
<div ng-if="address.type == 'ONLINE'">
<select name="appName" ng-model="locationsAddress.address[$index].appName" required>
<option value="" selected="selected">App Type</option>
<option value="SKYPE">Skype</option>
<option value="WHATSAPP">WhatsAPP</option>
</select>
<div ng-show="form.$submitted || locationForm.appName.$touched">
<span class="error" ng-show="locationForm.appName.$error.required">Chat required.</span>
</div>
</div>
</ng-form>
</div>
<button type="submit" class="button">Save</button>
</form>
关于angularjs - 如何在选择选项更改时重置表单字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41426197/
我尝试通过正则表达式将文本中的单引号更改为双引号。 (单字)示例:我走了。 You gona fly to planet 'Ziqtos' => 我需要在 I'm 中保留单引号,并在 You gona
我正在构建一个 API,其中大部分将包含 JSON 和 HTML 内容。但是一些非常具体的端点只呈现 true 或 false,并且还在 POST 中接受 true 或 false。这是请求或响应的整
我是一名优秀的程序员,十分优秀!