簡體   English   中英

如何使用AngularJS獲取文本字段的值?

[英]How do I get the value of a text field using AngularJS?

我想在提交表單時使用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);
};

代替eventSearchKeyWord作為參數傳遞

ng-submit="AdvanceSearchSubmit(SearchKeyWord)"

調節器

$scope.AdvanceSearchSubmit = function(keyWord)
{
    alert(keyWord);
};

提交時,您根本不需要將事件傳遞給AdvanceSearchSubmit 您已經在$scope值,例如ng-model用於輸入字段,例如ng-model="SearchKeyWord"

alert($scope.SearchKeyWord); //access value from `$scope` directly

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM