2017-04-08 28 views
0

我有一個列出所有用戶發票的表格,並且每個發票都包含一個付款按鈕以允許他們支付賬單。使用JavaScript來抓取多個ID的

我目前有一個循環遍歷每張發票並在自己的行中顯示每張發票,當用戶去點擊付款按鈕時,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> 
+2

真的直線前進。您嘗試在頁面上多次使用ID。 ID是獨一無二的。改爲使用帶有'querySelectorAll()'的類。 – Ohgodwhy

+0

謝謝你的迴應!我不知道這些身份證,以及他們應該如何獨特,非常好的東西!那麼,我怎麼才能得到被點擊的行的索引值,以顯示正確的div集?你能解釋還是展示一個例子? – rapid3642

回答

0

建議:使用jQuery。對於這樣的情況,它顯着簡化了js函數的一部分。

的jsfiddle解決方案:https://jsfiddle.net/brednmuc/4/

簡要說明: 移動使用的一切HTML標識走,只是讓屬性,這些屬性使最適合您的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>