简体   繁体   English

如何在angularjs中提交没有表单的单选框值

[英]How to submit radio box values without a form in angularjs

I am displaying two json files data in two tables. 我在两个表中显示两个json文件数据。 I am able to console.log() the value of each row selected. 我可以console.log()选择的每一行的值。 I made a submit button, but I am not sure how to get those two values to submit it. 我做了一个提交按钮,但是我不确定如何获取这两个值来提交它。 Does someone could help me to understand how to do that ? 有人可以帮助我了解如何做吗?

Below are my two tables 以下是我的两张桌子

    <div class="row">
        <div class="col-md-8">
            <h1>Choose your outbound</h1>
            <table class="table">
                <tr>
                    <th>Departure</th>
                    <th>Arrival</th>
                    <th>Economy class</th>
                    <th>Business class</th>
                    <th>First class</th>
                    <th></th>
                </tr>
                <tr ng-repeat="outbound in outbounds" ng-class="{active : isSelected(outbound)}" ng-click="setMaster(outbound)">          
                    <td>{{ outbound.departure }}h</td>
                    <td>{{ outbound.arrival }}h</td>
                    <td>{{ outbound.ecoPrice }}</td>
                    <td>{{ outbound.businessPrice }}</td>
                    <td>{{ outbound.firstPrice }}</td>
                    <td>
                        <input type="radio" name="radioOutbound">
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div class="row">
        <div class="col-md-8">
            <h1>Choose your inbound</h1>
            <table class="table">
                <tr>
                    <th>Departure</th>
                    <th>Arrival</th>
                    <th>Economy class</th>
                    <th>Business class</th>
                    <th>First class</th>
                    <th></th>
                </tr>
                <tr ng-repeat="inbound in inbounds" ng-class="{active : isSelected(inbound)}" ng-click="setMaster(inbound)">  
                    <td>{{ inbound.departure }}h</td>
                    <td>{{ inbound.arrival }}h</td>
                    <td>{{ inbound.ecoPrice }}</td>
                    <td>{{ inbound.businessPrice }}</td>
                    <td>{{ inbound.firstPrice }}</td>
                    <td>
                        <input type="radio" name="radioInbound">
                    </td>
                </tr>
            </table>
            <button type="submit" ng-click="submit()">Submit</button>
        </div>
    </div>

Below is my AngularJS 下面是我的AngularJS

// Inbound
$scope.isSelected = function(inbound) {
    return $scope.selected === inbound;
}

$scope.setMaster = function(inbound) {
    $scope.selected = inbound;
    console.log($scope.selected);
}

// Outbound
$scope.isSelected = function(outbound) {
    return $scope.selected === outbound;
}

$scope.setMaster = function(outbound) {
    $scope.selected = outbound;
    console.log($scope.selected);
}

// Submit
$scope.submit = function() {
    console.log($scope.selected);
}

add ng-model to radio button ng-model添加到单选按钮

<td>
   <input type="radio" name="radioOutbound" ng-model="radio_value1">
</td>

and for the second one also 第二个也是

<td>
   <input type="radio" name="radioInbound" ng-model="radio_value2">
</td>

in the submit function you can pass the two values 在Submit函数中,您可以传递两个值

<button type="submit" ng-click="submit(radio_value1, radio_value2)" >Submit</button>

in the controller 在控制器中

$scope.submit = function(val1, val2){
  console.log(val1, val2);
}

if you dont want to pass values in the function then values will be in $scope.radio_value1 and $scope.radio_value2 如果您不想在函数中传递值,则值将在$scope.radio_value1$scope.radio_value2

I've created a plunker that I believe demonstrates your desired goal. 我创造了一个矮人,相信可以证明您的目标。 My understanding from your code was: 我对您的代码的理解是:

  • You wanted either the radio button or the row clicked to select the item. 您需要单选按钮或单击行以选择项目。 The inbound and outbound items were each seperate sections, and only one radio in each should be selected. 入站和出站项目分别是独立的部分,每个中仅应选择一个无线电。
  • All data should be returned back to the controller, once selected. 选定后,所有数据应返回给控制器。 This will send it all back in one objected, broken down by the model. 这会将其全部发送回一个对象,并按模型分解。

Within $scope.submit, you will find the selected option for inbound on formData.radioOutbound, and on formData.radioOutbound for the outbound item. 在$ scope.submit中,您将在formData.radioOutbound上找到入站的选定选项,并在outData的formData.radioOutbound上找到选定的入站选项。

https://plnkr.co/edit/iwHi5NEDJSahG0JJ31ih?p=preview https://plnkr.co/edit/iwHi5NEDJSahG0JJ31ih?p=preview

JS // Submit $scope.submit = function(formData) { console.log(formData); JS //提交$ scope.submit = function(formData){console.log(formData); } }

  $scope.formData = {
    radioOutbound: '',
    radioInbound: ''
  };

  $scope.outbounds = [
      {
        departure: 'today',
        arrival: 'tomorrow',
        ecoPrice: 100,
        businessPrice: 200,
        firstPrice: 300
      },
      {
        departure: 'tomorrow',
        arrival: 'next day',
        ecoPrice: 100,
        businessPrice: 200,
        firstPrice: 300
      }
    ]

    $scope.inbounds = [
      {
        departure: 'today',
        arrival: 'tomorrow',
        ecoPrice: 100,
        businessPrice: 200,
        firstPrice: 300
      },
      {
        departure: 'tomorrow',
        arrival: 'next day',
        ecoPrice: 100,
        businessPrice: 200,
        firstPrice: 300
      }
    ]

HTML 的HTML

 <div class="row" >
        <div class="col-md-8">
            <h1>Choose your outbound</h1>
            <table class="table">
                <tr>
                    <th>Departure</th>
                    <th>Arrival</th>
                    <th>Economy class</th>
                    <th>Business class</th>
                    <th>First class</th>
                    <th></th>
                </tr>
                <tr ng-repeat="outbound in outbounds"
                    ng-class="{'success' : formData.radioOutbound == this.outbound}"
                    ng-click="formData.radioOutbound = this.outbound">

                    <td>{{ outbound.departure }}h</td>
                    <td>{{ outbound.arrival }}</td>
                    <td>{{ outbound.ecoPrice }}</td>
                    <td>{{ outbound.businessPrice }}</td>
                    <td>{{ outbound.firstPrice }}</td>
                    <td>
                        <input type="radio" ng-value="outbound" ng-model="formData.radioOutbound">
                    </td>
                </tr>
            </table>
        </div>
    </div>
    <div class="row">
        <div class="col-md-8">
            <h1>Choose your inbound</h1>
            <table class="table">
                <tr>
                    <th>Departure</th>
                    <th>Arrival</th>
                    <th>Economy class</th>
                    <th>Business class</th>
                    <th>First class</th>
                    <th></th>
                </tr>
                <tr ng-repeat="inbound in inbounds"
                    ng-class="{'success' : formData.radioInbound == this.inbound}"
                    ng-click="formData.radioInbound = this.inbound">  
                    <td>{{ inbound.departure }}h</td>
                    <td>{{ inbound.arrival }}h</td>
                    <td>{{ inbound.ecoPrice }}</td>
                    <td>{{ inbound.businessPrice }}</td>
                    <td>{{ inbound.firstPrice }}</td>
                    <td>
                        <input type="radio" ng-value="inbound" ng-model="formData.radioInbound">
                    </td>
                </tr>
            </table>
            <button type="submit" ng-click="submit(formData)">Submit</button>
        </div>
    </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM