【问题标题】:checkbox limitation with ng-model in angularjsangularjs中ng-model的复选框限制
【发布时间】:2014-04-04 10:13:46
【问题描述】:

到目前为止,我只看到有人使用 ng-model 来绑定复选框的状态。我怎么知道哪个复选框被angularjs选中了?例如,我想获取选中复选框的索引,以便我可以在后端做一些事情。

如果它在 jquery / js 中,我可以使用函数来捕获复选框的状态并将索引或其他信息发送回数据库。

我的笨蛋:http://plnkr.co/edit/dG9cwswiVzLdjEnpSNvu?p=preview

【问题讨论】:

  • 看来您已经使用正常了。你到底想做什么?
  • 如果复选框被选中item.done 将是true,是不是不够好?
  • @JayShukla 这只是一些 css,如果我想运行一些功能怎么办?例如获取选中复选框的名称?我需要将这些数据传递到后端。

标签: javascript angularjs checkbox


【解决方案1】:

你可以使用ng-click,像这样:

http://plnkr.co/edit/kOTtbSHbw1kaGWmjqQbf?p=preview

【讨论】:

  • 大声笑,这太简单了,谢谢。我想用 ng-model 传递参数,这很愚蠢
  • 我可以传递多个参数吗?像 ng-click="boxChecked({{groupId}},$index)"
  • 是的,你可以。我误解了你的问题。 :)
  • 你不需要 {{ }},所以试试吧 ng-click="boxChecked(groupId,$index)
  • 其实是这样的: item.groupId ,所以我放了 {{}}
【解决方案2】:

我不清楚你的问题,但如果你想要与复选框关联的标签,那么你应该使用ng-init

<div ng-controller="main">
    <div class="checkbox" ng-repeat="item in items">
         <input type="checkbox" id="{{$index}}" ng-model="item.done"><label for="{{$index}}" class="done-{{item.done}}" ng-init="item.text='Label for' + item.val">Label for {{item.val}}</label>
    </div>
    {{items}}
</div>

See Demo

【讨论】:

  • 感谢您指出杰。如何像这样传递多个参数 ng-click="boxChecked({{groupId}},$index)" ??
猜你喜欢
  • 2016-02-25
  • 1970-01-01
  • 2017-01-31
  • 2017-08-13
  • 2018-02-21
  • 2023-03-18
  • 2015-08-27
  • 2016-10-10
  • 1970-01-01
相关资源
最近更新 更多