使用jquery获取文本框的值

时间:2013-12-04 09:08:25

标签: php jquery html codeigniter

我有以下脚本:

<script>

    $(document).ready(function() {

        $('#edit').click(function (){
            //get
            employee_id = $('#employee_id').val();
            alert(employee_id);
            html='';

            $.ajax({
                type: "GET",
                url: "<?php echo base_url();?>administrator/admin/get_employee_details/"+employee_id,
                dataType: "json",
                success: function(response) {
                    console.log(response);
                    $( '#fname' ).val(response[0].f_name);
                    alert(response[0].amount);
                    $( '#sname' ).val(response[0].l_name);
                    $( '#lname' ).val(response[0].other_name);
                    $( '#expiry_date' ).val(response[0].id_no);
                    $( '#quantity_available').val(response[0].dob)
                    $( '#gender' ).val(response[0].gender);
                    $( '#maritalstatus').val(response[0].marital_status)
                    $( '#sex' ).val(response[0].gender);
                },
                error: function(data){
                }
            })
        });

    });

</script>

该脚本应该从下表中选择employee_id值:

<tbody>
    <?php foreach($employee_details as $employee ):?>
        <tr class="odd gradeX">
            <td><?php echo $employee['employee_name'];?></td>
            <td ><?php echo $employee['id_no'];?></td>
            <td><?php echo $employee['dob'];?></td>
            <td><?php echo $employee['gender'];?></td>
            <td ><?php echo $employee['marital_status'];?></td>
            <td><?php echo $employee['phone_no'];?></td>
            <td><?php echo $employee['email'];?></td>
            <td ><?php echo $employee['date_added'];?></td>
            <td><?php echo $employee['residence'];?></td>
            <td ><?php echo $employee['next_of_kin_name'];?></td>
            <td><?php echo $employee['next_of_kin_relation'];?></td>
            <td><?php echo $employee['next_of_kin_phone_no'];?></td>
            <td ><?php echo $employee['is_active'];?></td>
            <td><?php echo $employee['department_name'];?></td>
            <td><?php echo $employee['employee_class'];?></td>
            <td><a class="edit" href="#edit_details" id="edit">Edit </a></td>
            <td> <a class="delete" href="#delete_details" id="delete"  > Delete Employee</a></td>
            <input type="text" name="employee_id" id="employee_id" value="<?php echo $employee['employee_id'];?>"/>
        </tr>
    <?php endforeach;?>
</tbody>
单击“编辑”按钮时,

是表中的隐藏文本字段(employee_id)。但是当我点击链接时,我得到一个没有employee_id的空结果,但我可以在文本框中看到员工ID。如何从每个特定行的文本框中获取员工ID?

3 个答案:

答案 0 :(得分:0)

id属性在您的网页中必须是唯一的。您不能拥有2个具有相同id的元素。

因此,对于您的链接和输入,您必须为每一行生成不同的ID。例如,您可以尝试id="employee_id-<?php echo $employee['employee_id'];?>"。 但是你可以为每个链接和输入保留相同的类,并在你的javascript脚本代码中使用它。

然后你必须改变你的代码:

$('.edit').click(function() {
    var employee_id = this.closest('tr').find('.employee_id').val();
});

答案 1 :(得分:0)

元素的ID必须是唯一的,因此在循环中使用class而不是id,同时将employee_id字段移动到td元素

<td> 
    <a class="edit" href="#edit_details">Edit </a>
</td>
<td> 
    <a class="delete" href="#delete_details" > Delete Employee</a>
    <input type="text" name="employee_id" value="<?php echo $employee['employee_id'];?>" />
</td>

然后

$(document).ready(function () {


    $('.edit').click(function () {


        //get
        var employee_id = $(this).closest('tr').find('input[name="employee_id"]').val();
        alert(employee_id);
        html = '';

        $.ajax({
            type: "GET",
            url: "<?php echo base_url();?>administrator/admin/get_employee_details/" + employee_id,
            dataType: "json",
            success: function (response) {
                console.log(response);
                $('#fname').val(response[0].f_name);
                alert(response[0].amount);
                $('#sname').val(response[0].l_name);
                $('#lname').val(response[0].other_name);
                $('#expiry_date').val(response[0].id_no);
                $('#quantity_available').val(response[0].dob)
                $('#gender').val(response[0].gender);
                $('#maritalstatus').val(response[0].marital_status)
                $('#sex').val(response[0].gender);

            },
            error: function (data) {

            }
        })

    });


});

答案 2 :(得分:0)

 Use class attribute instead of id because id cant be repeated.

<input type="text" name="employee_id" class="employee_id" value="<?php echo $employee['employee_id'];?>"/>


$('.edit').click(function() {
    var id = this.closest('tr').find('input.employee_id').val();
});

如果您能够获得员工ID