【问题标题】:Get form name by the click on submit button通过单击提交按钮获取表单名称
【发布时间】:2015-03-17 08:59:23
【问题描述】:

    app.directive('customToaster', function () {

        return {
            link: function (scope, element, attrs, ctrl) {

                var foundButton = document.getElementByIds("buttonName2");    

                  foundButton.bind('click', function () {
                    scope.$apply(function () {
                      console.log('some text');
                    });
                });
              });
<form name="someName2">
 <div> 
  <div> 
   <button type="submit" name="buttonName2">Submit2 </button>
  </div>
 </div>
</form>

如何使用 AngularJS 通过单击提交按钮来获取表单名称。

<form name="someName">
 <div> 
  <div> 
   <button type="submit">Submit </button>
  </div>
 </div>
</form>

请记住,嵌套的 div 标签的数量可以超过两个。 谢谢!

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:
        app.directive('customToaster', function () {
    
        return {
            link: function (scope, element, attrs, ctrl) {
    
                    $scope.inputClick = function(name) {
                        console.log("form name: " + name);
                    }
             }
        }
        });
    
    <form ng-model="someName2">
    <div> 
        <div> 
        <button ng-click="inputClick(someName2)" type="submit" name="buttonName2">Submit2 </button>
        </div>
    </div>
    </form>
    

    【讨论】:

    • 实际上我创建了指令,并且在此按钮上有一个事件“单击”。我想知道此按钮应用到的表单名称。像这样var foundButton = element.find("button"); foundButton.bind('click', function () { ...}
    • 所以将 ngModel 作为点击事件的参数传递给指令
    • 只需在指令中创建一个函数,然后使用 ngModel 参数将 ng-click 附加到输入标签
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-07
    • 2013-03-24
    • 2013-09-17
    • 2014-10-31
    • 1970-01-01
    • 2011-12-28
    相关资源
    最近更新 更多