我的ajax在添加乘客视图中不起作用,但是当我在其他视图(例如添加组乘客)上尝试时,它就起作用了。我正在使用Codeigniter。
这是我的ajax:
$(document).ready(function(){
$("#destination").change(function(){
var destination = $("#destination").val();
var region = $("#region").val();
$.ajax({
url: 'http://localhost/ci-travel2.0/passanger/getdateprice/',
type: 'POST',
dataType: 'json',
data: {
destination : destination,
region : region
},
success: function (data) {
$("#date_tour").val(data.date_tour);
if((<?= $this->session->userdata('jumlah_pax') ?>) && (<?= $id_user ?> >= 1)) {
$("#price_invoice").val(data.price*<?= $this->session->userdata('jumlah_pax') ?>-500000*<?= $this->session->userdata('jumlah_pax') ?>);
} else if((<?= $this->session->userdata('jumlah_pax') ?>) && (<?= $id_user ?> <= 0)){
$("#price_invoice").val(data.price*<?= $this->session->userdata('jumlah_pax') ?>-250000*<?= $this->session->userdata('jumlah_pax') ?>);
} else if((<?= $id_user ?>) >= 1 && !(<?= $this->session->userdata('jumlah_pax') ?>)){
$("#price_invoice").val(data.price-500000);
} else if((<?= $id_user ?>) <= 0 && !(<?= $this->session->userdata('jumlah_pax') ?>)){
$("#price_invoice").val(data.price-250000);
}
}
});
});
});
这是我的第一个视图(附加乘客)
<div class="col-md-12">
<form action="<?= base_url('passanger/addpassanger') ?>" method="POST" enctype="multipart/form-data">
<div class="form-row">
<div class="col-md-3">
<label for="region">Region</label>
<select name="region" id="region" class="select2-input region form-control" style="width:100%;">
<option value="">-- Select Region --</option>
<?php foreach($region as $r) : ?>
<option value="<?= $r['id_region'] ?>"><?= $r['region'] ?></option>
<?php endforeach; ?>
<?= form_error('region', '<small class="text-danger pl-3">', '</small>'); ?>
</select>
</div>
<div class="col-md-3">
<label for="destination">Destination</label>
<select name="destination" id="destination" class="select2-input destination form-control" style="width:100%;">
<option value="">-- Select Destination --</option>
<?php foreach($destination as $d) : ?>
<option value="<?= $d['id_destination'] ?>"><?= $d['destination'] ?></option>
<?php endforeach; ?>
<?= form_error('destination', '<small class="text-danger pl-3">', '</small>'); ?>
</select>
</div>
<div class="col-md-3">
<label for="date_tour">Tanggal Keberangkatan</label>
<input type="text" class="form-control" id="date_tour" name="date_tour" readonly>
<?= form_error('date_tour', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
<div class="col-md-3">
<label for="price">Price Invoice</label>
<input type="text" class="form-control" id="price_invoice" name="price_invoice" readonly>
<?= form_error('price_invoice', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
</div>
这是我的第二个观点(组乘客)
div class="col-md-12">
<form action="<?= base_url('passanger/addgroup') ?>" method="POST" enctype="multipart/form-data">
<div class="form-row">
<div class="col-md-3">
<label for="region">Region</label>
<select name="region" id="region" class="select2-input region form-control" style="width:100%;">
<option value="">-- Select Region --</option>
<?php foreach($region as $r) : ?>
<option value="<?= $r['id_region'] ?>"><?= $r['region'] ?></option>
<?php endforeach; ?>
<?= form_error('region', '<small class="text-danger pl-3">', '</small>'); ?>
</select>
</div>
<div class="col-md-3">
<label for="destination">Destination</label>
<select name="destination" id="destination" class="select2-input destination form-control" style="width:100%;">
<option value="">-- Select Destination --</option>
<?php foreach($destination as $d) : ?>
<option value="<?= $d['id_destination'] ?>"><?= $d['destination'] ?></option>
<?php endforeach; ?>
<?= form_error('destination', '<small class="text-danger pl-3">', '</small>'); ?>
</select>
</div>
<div class="col-md-3">
<label for="date_tour">Tanggal Keberangkatan</label>
<input type="text" class="form-control" id="date_tour" name="date_tour" readonly>
<?= form_error('date_tour', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
<div class="col-md-3">
<label for="price">Price Invoice</label>
<input type="text" class="form-control" id="price_invoice" name="price_invoice" readonly>
<?= form_error('price_invoice', '<small class="text-danger pl-3">', '</small>'); ?>
</div>
</div>
两种观点之间没有区别(我认为),为什么一种有效而另一种无效?