Сбросить значение модального ввода при закрытии с помощью jQuery и AJAX - PullRequest
0 голосов
/ 08 октября 2018

У меня есть модал, который показывает данные, эти значения получены из значения результата моей функции $ .each.

Я хотел бы сбросить данные из моего модального окна, когда я его закрыл.

Вот мой jQuery с моим результатом ajax

 $('.edit').on('click', function(e){
   e.preventDefault();
  $('#edit_check').modal('show');
  var payment_id = $(this).attr('payment');
  var transaction_id = $(this).attr('transaction');
  var payment_no = $(this).attr('number');

  $.ajax({
      type: 'POST',
      url: 'transaction_row.php',
      data:
      {
        payment_no:payment_no,
        payment_id:payment_id,
        transaction_id:transaction_id
      },
      dataType: 'json',
      success: function(result)
      {
        //console.log(result);
        $.each(result, function(index, value){
          //client details
          $('#client').val(value.fullName);
          $('#company').val(value.company_name);
          $('#address').val(value.company_address);
          $('#trans_number').val(value.payment_transaction_no);
          //transaction details
          $('#edit_bank_name').val(value.bank_name);
          $('#edit_bank_branch').val(value.bank_branch);
          $('#edit_amount').val(value.payment_amount);
          $('#edit_check_number').val(value.check_no);
          $('#edit_check_date').val(value.checkDate);
        }); 
      }
  });

Вот мой html / модальный

 <div class="modal fade" id="edit_check">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
          <h3 class="modal-title"> Edit Check Transaction </h3>
          <br>  
          <i>Client</i>:&nbsp;<span><strong> <input type="text" id="client" name="client_name" class="noBorder" readonly> </strong></span>
          <br>
          <i>Company</i>:&nbsp;<span><strong> <input type="text" id="company" name="company_name" class="noBorder" readonly> </strong></span> 
          <br>
          <i>Address</i>:&nbsp;<span><strong> <input type="text" id="address" name="company_address" class="noBorder" readonly> </strong></span>
          <br>
          <i>Transaction Number</i>:&nbsp;<span><strong> <input type="text" id="trans_number" name="trans_number" class="noBorder" readonly> </strong></span>
          <br>
          <br>

          <div class="form-group">
            <label for="payment_method" class="col-sm-3 control-label"> Edit Payment Method: </label>
            <div class="col-sm-9">
              <input type="hidden" id="show_method_id">
            <select id="payment_method" name="payment_method" class="form-control" required>
              <option id="1" value="cash">Cash</option>
              <option id="2" value="ocash">Online Cash</option>
              <option id="3" value="check" selected>Check</option>
            </select>
            </div>
          </div>

          <div class="form-group name">
            <label for="edit_bank_name" class="col-sm-3 control-label">Bank Name:</label>
              <div class="col-sm-9">
                 <input type="text" class="form-control" id="edit_bank_name" name="edit_bank_name" placeholder="Enter Bank Name" required>
              </div>
          </div>

          <div class="form-group branch">
            <label for="edit_bank_branch" class="col-sm-3 control-label">Bank Branch:</label>
              <div class="col-sm-9">
                 <input type="text" class="form-control" id="edit_bank_branch" name="edit_bank_branch" placeholder="Enter Bank Branch" required>
              </div>
          </div>

            <div class="form-group amount">
              <label for="edit_amount" class="col-sm-3 control-label">Amount:</label>
                <div class="col-sm-9">
                  <input type="text" class="form-control" id="edit_amount" name="edit_amount" placeholder="Enter Amount" maxlength="13"  
                  value="₱" required>
                </div>
            </div>

            <div class="form-group number">
              <label for="edit_check_number" class="col-sm-3 control-label">Check Number:</label>
                <div class="col-sm-9">
                  <input type="number" class="form-control" id="edit_check_number" name="edit_check_number" min="0" maxlength="9" 
                  placeholder="Enter Check Number" required>
                </div>
            </div>

            <div class="form-group date">
              <label for="edit_check_date" class="col-sm-3 control-label">Date:</label>
                <div class="col-sm-9">
                  <input type="text" class="form-control" id="edit_check_date" name="edit_check_date" placeholder="" required>
                </div>
            </div>

          <div class="modal-footer">
             <button type="button" class="btn btn-default btn-flat pull-left" data-dismiss="modal">
              <i class="fa fa-close"></i> Close
            </button>
            <button type="submit" id="editCheckTransactionBtn" name="editTransactionBtn" class="btn btn-success btn-flat" >
              <i class="fa fa-edit"></i> Edit Details
            </button>
          </div>

      </div>
    </div>
  </div>
</div>

Я не могу найти ни одного уместного вопроса / ответа по моей проблеме.

Обновление

Я использовал $('#myFormModal').trigger("reset"), но ввод скрытия не отображался.

Должно быть так:

enter image description here

Не так:

enter image description here

Я использую шоу и скрытие ввода следующим образом:

 if (payment_method == 1)
 {
   $('#input').show;
   $('#input').hide;
 }

Where 'check' is payment_method == 3

Ответы [ 2 ]

0 голосов
/ 08 октября 2018

вы можете написать логику для очистки полей при закрытии модального события, как показано ниже

$("#edit_check").on("hidden.bs.modal", function () {
    // reset all your fields
          $('#client').val('');
          $('#company').val('');
          $('#address').val('');
          $('#trans_number').val('');
          //transaction details
          $('#edit_bank_name').val('');
          $('#edit_bank_branch').val('');
          $('#edit_amount').val('');
          $('#edit_check_number').val('');
          $('#edit_check_date').val('');
});
0 голосов
/ 08 октября 2018

Используйте функцию сброса с событием hidden.bs.modal,

// when modal dismissed
$('selector').on('hidden.bs.modal', function () {
    // Your reset function
    resetEverything()
})

Пожалуйста, проверьте эту скрипку для рабочего примера.

// Do something when after page is ready
$(document).ready(function(){
});
  
// When external edit button clicked, open modal
$('.edit').click( function(){
		$('#edit_check').modal('show');
});    


// Since you edit button is type submit
// you don't need separate event. You can
// user form submit event to track everything
$('#myFormModal').on('submit', function(e){
		e.preventDefault();
		// Do your work here

		// At the end reset form
    resetMyForm();
});

// Form reset function
function resetMyForm(){
		$('input').val('');
    $('#payment_method').val(0);
}

// If modal close reset everything
$("#edit_check").on("hidden.bs.modal", function () {
  	resetMyForm();
});
  .modal-confirm {
    color: #636363;
    width: 400px;
  }
    input.noBorder {
      border: none;
      background-color: transparent;
   }
  .modal-confirm .modal-content {
    padding: 20px;
    border-radius: 5px;
    border: none;
    text-align: center;
    font-size: 14px;
    width: 600px;
  }
  .modal-confirm1 .modal-content1 {
    padding: 20px;
    border-radius: 5px;
    border: none;
    text-align: center;
    font-size: 14px;
    width: 600px;
  }
  .modal-confirm .modal-header {
    border-bottom: none;
        position: relative;
  }
  .modal-confirm h4 {
    text-align: center;
    font-size: 26px;
    margin: 30px 0 -10px;
  }
  .modal-confirm .close {
        position: absolute;
    top: -5px;
    right: -2px;
  }
  .modal-confirm .modal-body {
    color: #999;
  }
  .modal-confirm .modal-footer {
    border: none;
    text-align: center;
    border-radius: 5px;
    font-size: 13px;
    padding: 10px 15px 25px;
  }
  .modal-confirm .modal-footer a {
    color: #999;
  }
  .modal-confirm .icon-box {
    width: 80px;
    height: 80px;
    margin: 0 auto;
    border-radius: 50%;
    z-index: 9;
    text-align: center;
    border: 3px solid #f15e5e;
  }
  .modal-confirm .icon-box i {
    color: #f15e5e;
    font-size: 46px;
    display: inline-block;
    margin-top: 13px;
  }
    .modal-confirm .btn {
        color: #fff;
        border-radius: 4px;
    background: #60c7c1;
    text-decoration: none;
    transition: all 0.4s;
        line-height: normal;
    min-width: 120px;
        border: none;
    min-height: 40px;
    border-radius: 3px;
    margin: 0 5px;
    outline: none !important;
    }
  .modal-content {
    width: 125%;
  }
  .modal-confirm .btn-info {
        background: #c1c1c1;
    }
    .modal-confirm .btn-info:hover, .modal-confirm .btn-info:focus {
        background: #a8a8a8;
    }
    .modal-confirm .btn-danger {
        background: #f15e5e;
    }
    .modal-confirm .btn-danger:hover, .modal-confirm .btn-danger:focus {
        background: #ee3535;
    }
  .trigger-btn {
    display: inline-block;
    margin: 100px auto;
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js"></script>




<div class="modal fade" id="edit_check">
      <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
              <form id="myFormModal">
              <h3 class="modal-title"> Edit Check Transaction </h3>
              <br>  
              <i>Client</i>:&nbsp;<span><strong> <input type="text" id="client" name="client_name" class="noBorder" readonly> </strong></span>
              <br>
              <i>Company</i>:&nbsp;<span><strong> <input type="text" id="company" name="company_name" class="noBorder" readonly> </strong></span> 
              <br>
              <i>Address</i>:&nbsp;<span><strong> <input type="text" id="address" name="company_address" class="noBorder" readonly> </strong></span>
              <br>
              <i>Transaction Number</i>:&nbsp;<span><strong> <input type="text" id="trans_number" name="trans_number" class="noBorder" readonly> </strong></span>
              <br>
              <br>

              <div class="form-group">
                <label for="payment_method" class="col-sm-3 control-label"> Edit Payment Method: </label>
                <div class="col-sm-9">
                  <input type="hidden" id="show_method_id">
                <select id="payment_method" name="payment_method" class="form-control" required>
                   <option id="0" value="0">Choose Payment Method</option>
                 <option id="1" value="cash">Cash</option>
                  <option id="2" value="ocash">Online Cash</option>
                  <option id="3" value="check" selected>Check</option>
                </select>
                </div>
              </div>

              <div class="form-group name">
                <label for="edit_bank_name" class="col-sm-3 control-label">Bank Name:</label>
                  <div class="col-sm-9">
                     <input type="text" class="form-control" id="edit_bank_name" name="edit_bank_name" placeholder="Enter Bank Name" required>
                  </div>
              </div>

              <div class="form-group branch">
                <label for="edit_bank_branch" class="col-sm-3 control-label">Bank Branch:</label>
                  <div class="col-sm-9">
                     <input type="text" class="form-control" id="edit_bank_branch" name="edit_bank_branch" placeholder="Enter Bank Branch" required>
                  </div>
              </div>

                <div class="form-group amount">
                  <label for="edit_amount" class="col-sm-3 control-label">Amount:</label>
                    <div class="col-sm-9">
                      <input type="text" class="form-control" id="edit_amount" name="edit_amount" placeholder="Enter Amount" maxlength="13"  
                      value="₱" required>
                    </div>
                </div>

                <div class="form-group number">
                  <label for="edit_check_number" class="col-sm-3 control-label">Check Number:</label>
                    <div class="col-sm-9">
                      <input type="number" class="form-control" id="edit_check_number" name="edit_check_number" min="0" maxlength="9" 
                      placeholder="Enter Check Number" required>
                    </div>
                </div>

                <div class="form-group date">
                  <label for="edit_check_date" class="col-sm-3 control-label">Date:</label>
                    <div class="col-sm-9">
                      <input type="text" class="form-control" id="edit_check_date" name="edit_check_date" placeholder="" required>
                    </div>
                </div>

              <div class="modal-footer">
                 <button type="button" class="btn btn-default btn-flat pull-left" id='close' data-dismiss="modal">
                  <i class="fa fa-close"></i> Close
                </button>
                <button type="submit" id="editCheckTransactionBtn" name="editTransactionBtn" class="btn btn-success btn-flat" >
                  <i class="fa fa-edit"></i> Edit Details
                </button>
              </div>

            </form>
          </div>
        </div>
      </div>
    </div>
    
    <button class="edit">Edit Modal</button>
...