0

我想在提交表单时使用 AngularJS 获取文本字段值的值。

下面的代码总是显示“未定义”。

html:

<form ng-submit="AdvanceSearchSubmit($event)" name="AdvanceSearch" novalidate>
 <p ng-show="!AdvanceSearch.SearchKeyWord.$pristine && AdvanceSearch.SearchKeyWord.$invalid" class="text-danger">Text Cannot Be Empty!</p>
  <div ng-hide="AdvanceSearchModel" class="input-group">
    <input name="SearchKeyWord" ng-model="SearchKeyWord" id="SearchKeyWord" class="form-control" placeholder="Search in timeline...." type="text" required>
      <span class="input-group-btn" ng-click="isAdvanceSearch='false'; SearchPost(0,'true')">
       <button ng-disabled="AdvanceSearch.$invalid" type="submit" name="search" id="search-btn" class="btn btn-flat">
        <i class="fa fa-search"></i>
       </button>
      </span>
     </div>
</form>

一次尝试:

$scope.AdvanceSearchSubmit = function(event)
{
    alert(event.target.value);
};

另一种尝试:

$scope.AdvanceSearchSubmit = function(event)
{
    alert(event.SearchKeyWord.value);
};
4

2 回答 2

0

而不是作为参数event传递SearchKeyWord

ng-submit="AdvanceSearchSubmit(SearchKeyWord)"

控制器

$scope.AdvanceSearchSubmit = function(keyWord)
{
    alert(keyWord);
};
于 2017-07-01T06:59:25.127 回答
0

您根本不需要将事件传递给您AdvanceSearchSubmit的提交。您已经在内部提供了值,$scope例如ng-model输入字段,例如ng-model="SearchKeyWord"

alert($scope.SearchKeyWord); //access value from `$scope` directly
于 2017-07-01T07:00:16.140 回答