doudula1974 2017-06-26 09:31
浏览 31
已采纳

如何从ajax返回中选择选择框中的值

Project code when changed , it will get the current selected pcode(Project code) id value and list the corresponding acode(Activity code) list in another select box(#acode) using ajax.

but when i try to list the value of particular value from mysql, pcode is listing and make if condition check with mysql record if match that pcode is select, but once change the pcode mysql acode instead of selecting acode it will list all acodes.

please help, what i wanna to do.

function check(t){
    var id = t.id;
    var val = t.options[t.selectedIndex].value;
     $.ajax({
     type: 'post',
     url: 'code_verify.php',
     data: {
      project_code:val
     },
         success: function (response) {
            document.getElementById("acode").innerHTML=response; 
         }
     });
}   
<p>My Database Name : timesheet</p>
<p>Table: projectcode</p>
<table width="250" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center">id</td>
    <td align="center">project</td>
    <td align="center">pcode</td>Pcode
  </tr>
  <tr>
    <td align="center">1</td>
    <td>Library Time</td>
    <td align="center">LB</td>
  </tr>
  <tr>
    <td align="center">2</td>
    <td>Leave</td>
    <td align="center">L</td>
  </tr>
</table>
<p>Table: activitycode</p>
<table width="250" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center">id</td>
    <td align="center">pcode</td>
    <td align="center">activity</td>
    <td align="center">acode</td>
    Pcode </tr>
  <tr>
    <td align="center">1</td>
    <td align="center">1</td>
    <td align="center">Bench</td>
    <td align="center">B</td>
  </tr>
  <tr>
    <td align="center">2</td>
    <td align="center">2</td>
    <td align="center">Sick Leave</td>
    <td align="center">Sl</td>
  </tr>
  <tr>
    <td align="center">3</td>
    <td align="center">2</td>
    <td align="center">Causal Leave</td>
    <td align="center">CL</td>
  </tr>
  <tr>
    <td align="center">4</td>
    <td align="center">2</td>
    <td align="center">Loss of Pay</td>
    <td align="center">LP</td>
  </tr>
</table>
<p>Table: user_timesheet</p>
<table width="250" border="1" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center">id</td>
    <td align="center">userid</td>
    <td align="center">pcode</td>
    <td align="center">acode</td>
</tr>
  <tr>
    <td align="center">1</td>
    <td align="center">1</td>
    <td align="center">1</td>
    <td align="center">1</td>
  </tr>
  <tr>
    <td align="center">2</td>
    <td align="center">1</td>
    <td align="center">2</td>
    <td align="center">2</td>
  </tr>
  <tr>
   <td align="center" bgcolor="#666666">3</td>
    <td align="center" bgcolor="#666666">1</td>
    <td align="center" bgcolor="#666666">2</td>
    <td align="center" bgcolor="#666666">3</td>
  </tr>
  <tr>
    <td align="center">4</td>
    <td align="center">1</td>
    <td align="center">2</td>
    <td align="center">4</td>
  </tr>
</table>
<p></p>
<p>&nbsp;</p>
<p>&nbsp; </p>
<table width="250" border="1" cellspacing="0" cellpadding="0">
<tr>
<td>
<select name="pcode_1" id="1" class="pcode" onChange="check(this);">
<?php

$sql=mysql_query("select * from user_timesheet where id=3");

while($row=mysql_fetch_array($sql)){

$leave=mysql_query("SELECT * FROM projectcode"); while($leave1=mysql_fetch_array($leave)) { ?>

<option value="<?=$leave1['id']?>" <?php if($leave1['id']==$row['pcode']){ echo "selected";}?>> <?=$leave1['pcode']?> </option>

<?php  } } ?>
</select></td>

<td><select name="acode" id="acode"></select></td>
</tr>
</table>


<?php
 $project_code = $_POST['project_code'];
 $find=mysql_query("select * from activitycode where pcode=$project_code");
 while($row=mysql_fetch_array($find))
 {
  echo "<option value='$row[id]'>".$row['acode']."</option>";
 }

?>
 


<p>&nbsp; </p>
<p>i am trying to list the record as below query <br/><br/>

" select * from user_timesheet where id=3 " </p>
<p>&nbsp;</p>
<strong><u> Output: (currently coming)</u></strong><br/><br/>
//While change Project code , ajax(using project code ID) return all activity code corresponding to pcode from activitycode table,
<br/><br/>
Then list record with user_timesheet where id=3, need to select automatic corrponding ID, pcode and acode wanna to select.
 <table width="250" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Project Code :
<select name="pcode_1" id="1" class="pcode" onChange="check(this);">
<option value="1">Library Time </option>
<option value="2" selected="selected">Leave</option>
</select></td>
<td>Activity Code:
<select name="acode" id="acode">
<option value="1">Sick Leave</option>
<option value="2">Causal Leave</option>
<option value="3">Loss of Pay</option>

</select></td>
</tr>
</table>
<br/><br/>
<strong><u> Expecting Output: </u></strong><br/><br/>
 <table width="250" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>Project Code :
<select name="pcode_1" id="1" onChange="check(this);">
<option value="1">Library Time </option>
<option value="2" selected="selected">Leave</option>
</select></td>
<td>
Activity Code:
<select name="acode" id="acode">
<option value="1">Sick Leave</option>
<option value="2" selected="selected">Causal Leave</option>
<option value="3">Loss of Pay</option>
</select></td>
</tr>
</table>

</div>
  • 写回答

2条回答 默认 最新

  • du155305 2017-06-26 09:35
    关注

    You must use value property

    document.getElementById("acode").value = response;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题