0
我不知道我的代碼有什麼問題,但它不起作用。將HTML輸入值發送到JavaScript
我的控制器:
public function add_user_driver()
{
if(!$this->user_permission->check_permission())return;
$plate_number = $this->input->post('plate_number');
$location_id = $this->input->post('location_id');
$data_user = array(
'plate_number' => $plate_number,
'location_id' => $location_id,
);
$this->db->insert('user_driver', $data_user);
echo json_encode(array("plate_number" => $plate_number, "location_id" => $location_id));
}
的觀點:
<div class="modal fade" id="usermodal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">USER_DRIVER</h4>
</div>
<div class="modal-body" id='add'>
<div class="row" id="form_pesan">
<input type="hidden" name="id" id="id" />
<div class="col-sm-6">
<input type="text" class="form-control input-lg" id="plate_number" name="plate_number" placeholder="Plate Number" required>
</div>
<div class="col-sm-6">
<input type="text" class="form-control input-lg" id="location_id" name="location_id" placeholder="Location Id" required>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-default" data-dismiss="modal">Close</button>
<button id="add" type="button" class="btn btn-primary btn-md" onclick=add()>Add</button>
</div>
</div>
<div id="form_submit_result"></div>
</div>
</div>
的JavaScript:
$(document).ready(function(){
matchFormFields = "#form_pesan input[required]";
matchFormSubmitResult = "#form_submit_result";
errorColor = 'red';
});
function add(){
var formIsValid = true;
$(matchFormFields).each(function() {
$(this).css('border-color', '');
if(!$.trim($(this).val())) {
$(this).css('border-color', errorColor);
formIsValid = false;
}
});
if (formIsValid) {
plate_number = $("#plate_number").val();
location_id = $("#location_id").val();
$.ajax
({
url : "<?php echo site_url('admin/add_user_driver')?>/",
type: "POST",
dataType: "text",
data:{plate_number: plate_number, location_id: location_id},
success: function(data)
{
$("#usermodal").modal("hide");
$("#alert").show();
//location.reload();
},
error: function (jqXHR, textStatus, errorThrown)
{
notify('Error data');
}
});
};
}
我要提交的輸入文本值的JavaScript add
功能,但不知何故, .val()
不會返回實際值。我做錯了什麼?
模型內部加上''
你檢查了控制檯嗎? –
'matchFormFields'這個變量是否定義?嘗試更改此數據:{plate_number:plate_number,location_id:location_id},'to data:{plate_number:$(「#plate_number」)。val(),location_id:$(「#location_id」)。val() },' – user6838959