如何根据其他字段值

时间:2015-10-12 12:47:40

标签: jquery html angularjs html5

我有这些输入字段:

<input class="text_field" id="ProfilePhoneNumber" phone-by-state="User.PhoneNumber" type="text" required/>

<div class="btn-group">
    <button multi-select="User.Notifications.Phone" class="btn btn-default dropdown-toggle">
        Select Severities <span class="caret"></span></button>
    <ul multi-select-dropdown class="dropdown-menu">
        <li>
            <input type="checkbox" id="NotificationsPhone_1"
                   ng-model="User.Notifications.Phone.High">
            <label for="NotificationsPhone_1">High</label>
        </li>
        <li>
            <input type="checkbox" id="NotificationsPhone_2"
                   ng-model="User.Notifications.Phone.Medium">
            <label for="NotificationsPhone_2">Medium</label>
        </li>
        <li>
            <input type="checkbox" id="NotificationsPhone_3"
                   ng-model="User.Notifications.Phone.Low">
            <label for="NotificationsPhone_3">Low</label>
        </li>
    </ul>
</div>

<div><input id="SMS" type="checkbox"
       ng-model="User.SMS.Enabled">SMS Enable
</div>

我想从ProfilePhoneNumber文本字段中删除'required'属性,并且只有在选中其他复选框中的至少一个时才需要它。

我可以通过HTML完成吗?角? jQuery的? 什么是最好的方式?

2 个答案:

答案 0 :(得分:5)

您可以在这里使用ng-required

<input class="text_field" id="ProfilePhoneNumber" phone-by-state="User.PhoneNumber" type="text" 
    ng-required="User.Notifications.Phone.High == 1 || User.Notifications.Phone.Medium == 1 || User.Notifications.Phone.Low == 1 || User.SMS.Enabled == 1" />
如果表达式传递给ng-required设置为required,则

ng-required会在元素上设置true属性。

答案 1 :(得分:0)

您可以使用angularjs“ng-required”

<input class="text_field" id="ProfilePhoneNumber" phone-by-state="User.PhoneNumber" type="text" ng-required="User.Notifications.Phone.High || User.Notifications.Phone.Medium || User.Notifications.Phone.Low"/>

<div class="btn-group">
    <button multi-select="User.Notifications.Phone" class="btn btn-default dropdown-toggle">
        Select Severities <span class="caret"></span></button>
    <ul multi-select-dropdown class="dropdown-menu">
        <li>
            <input type="checkbox" id="NotificationsPhone_1"
                   ng-model="User.Notifications.Phone.High">
            <label for="NotificationsPhone_1">High</label>
        </li>
        <li>
            <input type="checkbox" id="NotificationsPhone_2"
                   ng-model="User.Notifications.Phone.Medium">
            <label for="NotificationsPhone_2">Medium</label>
        </li>
        <li>
            <input type="checkbox" id="NotificationsPhone_3"
                   ng-model="User.Notifications.Phone.Low">
            <label for="NotificationsPhone_3">Low</label>
        </li>
    </ul>
</div>