使用javascript抓取多个ID

时间:2017-04-08 00:05:17

标签: javascript laravel getelementbyid

我有一个列出所有用户发票的表格,每张发票都包含一个付款按钮,以便他们支付账单。

我目前有一个循环,遍历每张发票并在每行中显示每张发票,当用户点击付款按钮时,JavaScript会显示一个选择框以使用已保存的卡或新卡,如果用户选择了已保存的卡,则JavaScript将显示包含其已保存卡的另一个选择框,如果选择了新卡,则将显示其他输入字段,此时显示和隐藏部分,当选择已保存的卡或新卡只适用于表格中的第一行,没有其他行可以使用该JavaScript,我确定它,因为JavaScript抓住了第一个ID并停在那里。当用户在每张发票上选择保存的卡或新卡时,如何正确地将其捕获到所有ID的位置并运行代码?

我创建了一个JSFiddle来显示我的确切情况,是否有人可以将其修改为可行的位置?我真的很感激! https://jsfiddle.net/s0fbrcw6/1/

payments.blade.php

@if($payments)
  @foreach($payments as $payment)
      <tr>
          <td><a href="">${{number_format(($payment->price /100), 2, '.', ' ')}}</a></td>
          <td>{{$payment->product_name}}</td>
          <td>{{$payment->created_at->toFormattedDateString()}}</td>
          <td>{{$payment->created_at->addMonth()->toFormattedDateString()}}</td>
          <td>{{$payment->reoccurring}}</td>
          <td>{{$payment->status}}</td>
            @if($payment->status == "Paid")
            @else
              <td>
                  <div class="add-payment-container">
                      <button class="toggle-add-payment mdl-button mdl-js-button mdl-js-ripple-effect pull-right btn-info">
                          @if($payment->reoccurring == "Yes")
                              Subscribe
                          @else
                              Pay Here
                          @endif
                      </button>
                      <div class="add-payment">
                          </br>
                          <form action="{{'/users/payment'}}" method="post"
                                id="checkout-form">
                              <input type="text" name="price" class="hidden"
                                     value="{{$payment->price}}">
                              <input type="text" name="productName" class="hidden"
                                     value="{{$payment->product_name}}">
                              <input type="text" name="paymentID" class="hidden"
                                     value="{{$payment->id}}">
                              <input type="text" name="reoccurring" class="hidden"
                                     value="{{$payment->reoccurring}}">
                              <div class="row">
                                  <div class="col-sm-4">
                                      <div id="paymentMethodDiv"
                                           class="form-group label-floating">
                                          {!! Form::label('paymentMethod', 'Payment Method') !!}
                                          </br>
                                          {!! Form::select('paymentMethod', ['Saved Card'=>'Saved Card','New Card'=>'New Card'], null, ['class' => 'browser-default mdl-selectfield', 'placeholder' => 'Choose Option', 'id' => 'paymentMethod'])!!}
                                      </div>
                                  </div>
                                  <div class="col-sm-4">
                                      <div id="savedCardsDiv" class="form-group label-floating" style="display: none;">
                                          {!! Form::label('card', 'Previous Cards') !!}
                                          </br>
                                          {!! Form::select('card', $cardLast4, null, ['class' => 'browser-default mdl-selectfield', 'placeholder' => 'Choose Option', 'id' => 'savedCards'])!!}
                                      </div>
                                  </div>
                                  <div class="col-md-4">
                                      <div id="cardHolderNameDiv" class="form-group label-floating" style="display: none;">
                                          <label class="control-label">Card Holder
                                              Name</label>
                                          <input type="text" id="card-name"
                                                 class="form-control">
                                      </div>
                                  </div>
                                  <div class="col-md-4">
                                      <div id="cardNumberDiv" class="form-group label-floating" style="display: none;">
                                          <label class="control-label">Card Number</label>
                                          <input type="text" id="card-number"
                                                 class="form-control">
                                      </div>
                                  </div>
                              </div>
                              <div class="row">
                                  <div class="col-md-5">
                                      <div id="expirationMonthDiv" class="form-group label-floating" style="display: none;">
                                          <label class="control-label">Expiration
                                              Month</label>
                                          <input type="text" id="card-expiry-month"
                                                 class="form-control">
                                      </div>
                                  </div>
                                  <div class="col-md-5">
                                      <div id="expirationYearDiv" class="form-group label-floating" style="display: none;">
                                          <label class="control-label">Expiration Year</label>
                                          <input type="text" id="card-expiry-year"
                                                 class="form-control">
                                      </div>
                                  </div>
                                  <div class="col-md-2">
                                      <div id="cvcDiv" class="form-group label-floating" style="display: none;">
                                          <label class="control-label">CVC</label>
                                          <input type="text" id="card-cvc"
                                                 class="form-control">
                                      </div>
                                  </div>
                              </div>
                              {{csrf_field()}}
                              <button type="submit" class="btn btn-primary pull-right">Make
                                  Payment
                              </button>
                              <div class="clearfix"></div>
                          </form>
                      </div>
                  </div>
              </td>
          @endif
      </tr>


<script>
    var paymentMethodSelect = document.getElementById('paymentMethod');
    paymentMethodSelect.onchange = function () {
        if (paymentMethodSelect.value == 'Saved Card') {
            document.getElementById("savedCardsDiv").style.display = "block";
            document.getElementById("cardHolderNameDiv").style.display = "none";
            document.getElementById("cardNumberDiv").style.display = "none";
            document.getElementById("expirationMonthDiv").style.display = "none";
            document.getElementById("expirationYearDiv").style.display = "none";
            document.getElementById("cvcDiv").style.display = "none";
        } else if (paymentMethodSelect.value == 'New Card') {
            document.getElementById("savedCardsDiv").style.display = "none";
            document.getElementById("cardHolderNameDiv").style.display = "block";
            document.getElementById("cardNumberDiv").style.display = "block";
            document.getElementById("expirationMonthDiv").style.display = "block";
            document.getElementById("expirationYearDiv").style.display = "block";
            document.getElementById("cvcDiv").style.display = "block";
        } else {
            document.getElementById("savedCardsDiv").style.display = "none";
            document.getElementById("cardHolderNameDiv").style.display = "none";
            document.getElementById("cardNumberDiv").style.display = "none";
            document.getElementById("expirationMonthDiv").style.display = "none";
            document.getElementById("expirationYearDiv").style.display = "none";
            document.getElementById("cvcDiv").style.display = "none";
        }
    };
</script>

1 个答案:

答案 0 :(得分:0)

建议:使用jQuery。对于这样的情况,它显着简化了js函数的一部分。

jsFiddle解决方案: https://jsfiddle.net/brednmuc/4/

简要说明: 不再为所有内容使用HTML ID,只需创建对您的Web应用程序最有意义的属性。此方法允许您实际定义无限数量的属性,您可以键入某些行为。这还可以防止与您可能​​使用/与项目集成的库发生冲突。

代码:
jQuery的:

 $(document).ready(function() {
    $("select[name='paymentMethod']").change(function() {
          var dropdownValue = $(this).val();
          var transactionId = $(this).parent().attr('transactionId');
          switch (dropdownValue) {
            case 'Saved Card':
                $("td[transactionId='" + transactionId + "'] div.savedCardsDiv").show();
              break;
            case 'New Card':
                $("td[transactionId='" + transactionId + "'] div.savedCardsDiv").hide();
              break;
            default:
                $("td[transactionId='" + transactionId + "'] div.savedCardsDiv").hide();
              break;
          };

        });
});

<强> HTML

<table>
<tr>
  <td transactionId="1">
    <select name="paymentMethod" form="carform" class="paymentMethod">
      <option value="">Select your option</option>
      <option value="Saved Card">Saved Card</option>
      <option value="New Card">New Card</option>
    </select>
    <div class="savedCardsDiv" style="display: none;">
      <select name="savedCards" form="carform" class="savedCards">
        <option value="">Select your option</option>
        <option value="Saved Card">****4242</option>
        <option value="New Card">****5423</option>
      </select>
    </div>
  </td>
</tr>
<tr>
  <td transactionId="2">
    <select name="paymentMethod" form="carform" class="paymentMethod">
      <option value="">Select your option</option>
      <option value="Saved Card">Saved Card</option>
      <option value="New Card">New Card</option>
    </select>
    <div class="savedCardsDiv" style="display: none;">
      <select name="savedCards" form="carform" class="savedCards">
        <option value="">Select your option</option>
        <option value="Saved Card">****4242</option>
        <option value="New Card">****5423</option>
      </select>
    </div>
  </td>
</tr>
</table>