将HTML输入值发送到JavaScript

时间:2016-09-21 07:16:18

标签: javascript php codeigniter

我不知道我的代码有什么问题,但是没有用。

我的控制器:

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">&times;</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()不返回实际值。我做错了什么?

2 个答案:

答案 0 :(得分:0)

var abc = $("#plate_number").val();
alert(abc);
return false;

您可以在javascript中使用变量abc

答案 1 :(得分:0)

请试试这个

datatype: "json" 

datatype: "text"
JSON.stringify({plate_number: plate_number, location_id: location_id})