在jQuery中使用change函数使用select-option获得多个动态类ID

时间:2019-07-04 11:40:27

标签: javascript jquery html

我正在尝试获取动态表行的类ID,第一行在被选中时运行良好,但是其他行正在从Java脚本中获取而无法正常工作。

即我无法从从Javascript表格行获取的行中获取类ID。

//this is jquery function am using....
 $('#course_code').click(function(){
      var course_id = $('#course_code').val();
      if(course_id != '')
      {
        $.ajax({
          url:"<?php echo base_url('Undergraduate_pg/fetch_course_code'); ?>",
          method:"POST",
          data:{course_id:course_id},
          success:function(data){
           
            $('#course_cod').val(data);
          }
        })
      }
    })

//this is the javascript table rows that is loading dynamically using click button..
function addrows(){
    var row = ($('.detail tr').length-0)+1;
    var tr = '<tr>' +
'<th>'+row+'</th>' +


'<td><select class="form-control course_code" id="course_code" name="course_code[]" required><option value="" selected="selected">Select Course Code</option><?php if (isset($pos)) { ?><?php foreach ($pos as $row) { ?>
  echo "<option> <?php echo $row->course_code; ?></option>";<?php } } ?></select></td>' +

'<td><select class="form-control course_title" id="Title"  name="course_title[]" required><option value="" selected="selected">Select Course Title</option><?php if (isset($pos)) { ?><?php foreach ($pos as $row) { ?>
  echo "<option> <?php echo $row->course_title; ?></option>";<?php } } ?></select></td>' +

'<td><input type="number" class="form-control unit_elect" id="course_units"  name="course_unit[]" value="" required></td>' +

'<td><select class="form-control course_status" id="course_status"  name="course_status[]" required><option value="" selected="selected">Select Course Status</option><?php if (isset($pos)) { ?><?php foreach ($pos as $row) { ?>
  echo "<option> <?php echo $row->course_status; ?></option>";<?php } } ?></select></td>' +


 '<td colspan="3"><a class="btn btn-danger remove" >X</a></td>'+ 
'</tr>';

$('.detail').append(tr);
    
  }

</script>




//i expect both static row and the dynamic row should be working once selected..
<!--this is the html code.. note, only this is working....-->
<tbody class="detail">

<td>1</td>
<td><select class="form-control course_code" id="course_code"  name="course_code" required>
  <option value="">--Select Course Code--</option>
                        <?php if (isset($pos)) { ?>
            <?php foreach ($pos as $row) { ?>
  

                        echo "<option value="<?php echo $row->course_id; ?>"> <?php echo $row->course_code; ?></option>";
                    <?php } } ?>
                 </select>
                 <input type="text" class="form-control course_cod" id="course_cod"  name="course_code[]" required>
 </td>

我希望静态行和动态行在选择后都可以正常工作。

0 个答案:

没有答案