如何在knockout foreach循环中验证所有控件?

时间:2018-02-02 10:24:10

标签: jquery knockout.js observablecollection

可观察数组:

var array=[
{List:[{Id:1,Name:'abc'}{Id:2,Name:'abc'}],UtilityId:0,AnnualUsage:'',Price:,Adder:},
{List:[{Id:1,Name:'abc'}{Id:2,Name:'abc'}],UtilityId:0,AnnualUsage:'',Price:,Adder:},
{List:[{Id:1,Name:'abc'}{Id:2,Name:'abc'}],UtilityId:0,AnnualUsage:'',Price:,Adder:}
]

淘汰赛绑定:

<tbody data-bind="foreach: NomatchAccounts.Accounts">
    <tr>
        <td>
            <select data-bind="options: Utility,value:UtilityId, optionsText: 'Name', optionsValue: 'Id',optionsCaption:'Choose Utiity..'"
                class="form-control"></select>
        </td>
        <td data-bind="text:AccountNumber"></td>
        <td>
            <input type="text" class="form-control" data-bind="value:Meter" />
        </td>
        <td>
            <input type="text" onkeypress="return isNumberKey(event)" class="form-control" data-bind="value:AnnualUsage" />
        </td>
        <td>
            <input type="text" onkeypress="return isNumberKey(event)" class="form-control" data-bind="value:Price" />
        </td>
        <td>
            <input type="text" onkeypress="return isNumberKey(event)" class="form-control" data-bind="value:Adder" />
        </td>
        <td data-bind="text:AdderProvided"></td>
    </tr>
</tbody>

enter image description here

我想验证所有文本框,Dropdown。任何人都可以向我建议我需要做什么。

1 个答案:

答案 0 :(得分:0)

您可以使用onfocusout事件。

<input type="text" onfocusout="myFunction()">

只要用户有新输入,它就会调用myFunction()并传入用户的新输入来执行验证。

或者您可以使用事件绑定

<div data-bind="event: { mouseover: enableDetails, mouseout: disableDetails }">

可以触发视图模型中定义的验证事件,基于按键,鼠标悬停或鼠标移除等事件。

以下链接供您参考! http://knockoutjs.com/documentation/event-binding.html