【问题标题】:Populate hidden field with the last selected value using angular.js使用 angular.js 使用最后选择的值填充隐藏字段
【发布时间】:2014-05-15 08:01:30
【问题描述】:

所以,我有这个代码Fiddle

<div ng-app="testApp">
<div ng-controller="MainCtrl">
    <form>
        <p>
            <select ng-model="selectedItem" ng-options="i.name for i in items"></select>
        </p>
        <p ng-show="selectedItem.subItems">
            <select ng-model="selectedSubItem" ng-options="i.name for i in selectedItem.subItems"></select>
        </p>

        <p ng-show="selectedSubItem.subItems">
            <select ng-model="selectedSubSubItem" ng-options="i.name for i in selectedSubItem.subItems"></select>
        </p>

        <input type="hidden" name="selection" value="????">

        <input type="submit" value="submit">
    </form>

</div>

并且在该代码中,如果所选项目具有子数组,我只会显示下一个下拉列表。 现在,我想使用 Angular.js 使用该下拉链的最后一个选定值填充隐藏字段的值。

换句话说,如果用户在最后出现的下拉菜单中选择一个选项,我希望最后的隐藏字段具有该值以与表单一起提交。

我该怎么做?

【问题讨论】:

    标签: javascript arrays angularjs filter angularjs-ng-repeat


    【解决方案1】:

    你可以使用:

    <input type="hidden" name="selection" value="{{selectedSubSubItem.name}}">
    

    一旦相应select 的值发生更改,双向绑定将更新隐藏输入的值。

    如果你想根据最后选择的动态获取它:

    <input type="hidden" name="selection" value="{{selectedSubSubItem.name || selectedSubItem.name || selectedItem.name}}">
    

    如果您希望能够回到链条中,这将有所帮助。但我强烈建议将此逻辑移至控制器:

    <input type="hidden" name="selection" value="{{(selectedSubItem.subItems ? selectedSubSubItem.name : false) || (selectedItem.subItems ? selectedSubItem.name : false) || selectedItem.name}}">
    

    【讨论】:

    • 但这只会用链的最后一个值填充隐藏字段。我想用用户选择的最后一个值填充隐藏字段。
    • @user1706198 好的,不清楚。但这很容易完成(见更新)。
    【解决方案2】:

    如果链的长度是固定的,则可以这样做:

    <input type="hidden" name="selection"
        value="{{ (selectedSubSubItem || selectedSubItem || selectedItem).name }}"/>
    

    要解决回溯到链的问题,一个简单的解决方案是使用ng-change 并取消所有下一个值,即:

    <select ng-model="selectedItem" ng-options="i.name for i in items"
        ng-change="selectedSubItem=null;selectedSubSubItem=null"></select>
    

    见分叉小提琴:http://jsfiddle.net/zH77p/1/


    替代ng-change,您可以在范围内使用手表;好处是手表只需要知道他们的直系孩子:

    $scope.$watch("selectedItem", function(newval, oldval) {
        if( newval !== oldval ) $scope.selectedSubItem = null;
    });
    $scope.$watch("selectedSubItem", function(newval, oldval) {
        if( newval !== oldval ) $scope.selectedSubSubItem = null;
    });
    

    【讨论】:

    • 这在链中向前运行,但向后移动时它会停留在最后一个值。
    • 效果很好,在链中倒退时可以,但是如果我在最后一个选项并更改了第一个选项,它会再次卡住,我将不得不回到链中一项一项才能正常工作。
    • 您是否像上面那样实现了ng-change,即更改第一个会使下一个两个无效,更改第二个会使第三个无效?在提供的小提琴中,在一直选择到第 3 后更改第 1 可以正常工作。手表也可以。您尝试了哪些具体步骤 - 以防我不理解您的问题?
    【解决方案3】:

    由于您对这种类型的“链式选择”感兴趣,因此您需要使用一些更复杂的逻辑。 这是小提琴:http://jsfiddle.net/2Az4P/3/ 我使用了 ng-value,但您可以保留您的值并将其设置为

    value="{{choiceValue()}}
    

    这将具有相同的效果。 我所做的是将答案放在一个数组 (selectedItem = []) 中,然后在我进行时填充它(注意每个选择的 ng-model)。

    在每次选择时,使用函数 choiceValue() 相应地更新值,该函数相应地更新 ng-value

    这样,您可以向链中添加尽可能多的选择,并且只需更新它们的数量(实际上,您可以为此使用ng-repeat,并使用$index 自动创建更多选择级别)。

    还有一些工作要做,但您可以在此处查看基础知识: http://jsfiddle.net/brendanowen/uXbn6/8/

    这完全取决于您需要执行此操作的复杂程度/自动化程度:)

    【讨论】:

    • 可行,但由于某种原因,它无法显示链中的第三个下拉列表。
    • 这非常有用。但是为什么在第一个小提琴中,只出现第二个下拉列表而不出现第三个?
    • 我已经更新了小提琴。我在变量名中有错字。这就是为什么使用 ng-repeat 来创建一系列选择可能非常方便 - 输入不多,因此拼写错误的机会更少:)
    • 选择最后一项“sub sub item 2”并跳到链中的第一个下拉菜单并选择“item 1”时出现问题,值卡在“sub sub item 2” ",第二个下拉菜单也不会消失!!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多