【问题标题】:Ionic Framework - How can I submit a form with dynamic content?Ionic Framework - 如何提交包含动态内容的表单?
【发布时间】:2016-02-12 00:01:12
【问题描述】:

我有一些使用 ng-repeat 动态生成的 texbox:

<ion-content>

    <div class="matchList">

        <div class="matchListItem" ng-repeat="match in matchData">

            <div class="home-team-name">
                {{match.Team1.TeamName}}
            </div>

            <div class="tip-input">
                <input type="text" style="border:1px solid #CCC; width:100%; height:23px; padding-left:4px;" />
            </div>

            <div class="tip-center">
                <center>:</center>
            </div>

            <div class="tip-input">
                <input type="text" style="border:1px solid #CCC; width:100%; height:23px; padding-left:4px;" />
            </div>

            <div class="guest-team-name">
                {{match.Team2.TeamName}}
            </div>

            <div style="clear:both"></div>
        </div>

    </div>

    <div class="save-button">
        <button class="button button-block button-positive" ng-click="save()">
            Save
        </button>
    </div>
</ion-content>

这是足球联赛的提示游戏。因此,比赛日的每场比赛都有两个文本框。我想将文本框的值发送到 php 文件。我知道我可以这样做:

$scope.save = function () {

    $http.get('http://localhost/saveTips.php?data=' + data).then(function (response) {
        $scope.doesItWork = response.data;
    })

}

但是如何将每个文本框的值保存在数据变量中以将其发送到 php 文件?

【问题讨论】:

    标签: javascript angularjs forms ionic submit


    【解决方案1】:

    您将在每个输入上使用ng-model,然后将数组发布到服务器。在服务器上,您必须接受 match 对象的数组。

    假设您的 match 对象是

    match:{
      Team1:{
        TeamName:'',
        InputValue:''
      },
      Team2:{
        TeamName:'',
        InputValue:''
      }
    }
    

    那么您的输入将有 ng-model="match.Team1.InputValue"(或 Team2)

    另外,您应该使用$http.post('http://localhost/saveTips.php', data) 并更改您的 php 文件以接受 POST 请求,因为看起来您正在保存数据。

    【讨论】:

      【解决方案2】:

      您需要将模型归因于字段,以允许访问控制器上的数据。 你可以做一些简单的事情,比如将数据存储在一个数组上(一个新的,甚至是你执行 ng-repeat 的同一个数组,如这个简单的例子所示。)

      <input type="text" ng-repeat="input in inputs" ng-model="input.model" />
      

      Live JSFiddle

      如果你想看一个更复杂的例子,请告诉我,但我认为你可以得到这个想法。

      【讨论】:

      • 我要提交第一个文本框的值应该怎么做?我试过了: var firstValue = $scope.matchData[0].model; $scope.save = function () { $http.get('localhost/saveTips.php?data=' + firstValue).then(function (response) { console.log(response.data); }) } 但它说:无法读取属性' 0' 未定义
      • 我已经更新了一些例子,take a look at it。如果这不是您要找的,请告诉我。 @诺诺
      猜你喜欢
      • 2015-08-10
      • 2017-04-27
      • 2012-07-21
      • 1970-01-01
      • 2021-05-21
      • 1970-01-01
      • 1970-01-01
      • 2011-08-22
      • 1970-01-01
      相关资源
      最近更新 更多