如何为我的 ajax 获取输入值?

how to get value in input for my ajax?

我想在此输入中获取值

<input id="<?php echo $p->id_pelajaran; ?>" name    ="nilai" type="number" class="form-control" onchange="nilaidetail('<?php echo $id->id_nilai; ?>','<?php echo $p->id_pelajaran; ?>')">

我的剧本

function nilaidetail(id_nilai,id_pelajaran){
            var nilai = $("#".id_pelajaran).val();
            $.ajax({
                 type : 'post',
                 url :'<?php echo base_url()?>index.php/Welcome/simpannilaidetail',
                 data :{'id_nilai':id_nilai,'id_pelajaran':id_pelajaran,'nilai':nilai},
                 dataType :'json',
                 success : function(response){
                 //alert(response);
                 //alert('NILAI TERSIMPAN');
                }

              });

        }

我想使用这个值插入我的数据库

public function simpannilaidetail(){
        $id_nilai = $this->input->post('id_nilai');
        $id_pelajaran = $this->input->post('id_pelajaran');
        $nilai = $this->input->post('nilai');
        $get = $this->modelpenilaian->datanilai($id_nilai, $id_pelajaran, $nilai);
        if (count($get) > 0) {
            $rs = "";
        }else{
            $nilaiawal = '0';
            $rs = $this->crudmodel->simpannilaifix($id_nilai,$id_pelajaran,$nilaiawal, $nilai); 
        }

        echo $rs;
    }

但是结果 $nilai 是 0 而不是我在

中输入的值

你的脚本var nilai = $("#".id_pelajaran).val();

function nilaidetail(id_nilai,id_pelajaran){
            var nilai = $("#".id_pelajaran).val();
            $.ajax({
                 type : 'post',
                 url :'<?php echo base_url()?>index.php/Welcome/simpannilaidetail',
                 data :{'id_nilai':id_nilai,'id_pelajaran':id_pelajaran,'nilai':nilai},
                 dataType :'json',
                 success : function(response){
                 //alert(response);
                 //alert('NILAI TERSIMPAN');
                }

              });

        }

改为var nilai = $('#'+id_pelajaran).val();

function nilaidetail(id_nilai,id_pelajaran){
            var nilai = $('#'+id_pelajaran).val();
            $.ajax({
                 type : 'post',
                 url :'<?php echo base_url()?>index.php/Welcome/simpannilaidetail',
                 data :{'id_nilai':id_nilai,'id_pelajaran':id_pelajaran,'nilai':nilai},
                 dataType :'json',
                 success : function(response){
                 //alert(response);
                 //alert('NILAI TERSIMPAN');
                }

              });

        }