dscqrkvr9562034621 2013-03-13 08:52
浏览 67
已采纳

php&js combobox触发事件

I have a problem with creating an script which will fill one combobox on base of what is selected in the other combobox.

I am newbie in the field of PHP / JS so please be patient with me. Here it is what I have done so far:

js script which is on click event

<script type="text/javascript">
    function selectDropdown(){
        var dropdownValue=document.getElementById("dropdown").value;
        $field=dropdownValue;
    }
</script>

Base combobox filled with values from database:

<select id="dropdown" name="dropdown" onchange="selectDropdown()">
    <option value="dd">--take an option--</option>
    <?
    $standard = Env::value('standard');
    if (!$status)
        $statement = "select code from standard";
    foreach ($db->query($statement )->fetchall() as $row)
        echo "<option".($row == $standard ? ' selected ' : '').">$row[0]</option>";



    ?>
</select>

and now I want to put the selected option into other combobox - actually I will execute another SQL query with the value chosen previously but the fact that I will have value in variable is more then enough for me to finish the task.

<select name='st' class='txt'>
  <option value="st"><? echo $field; ?></option>
</select>

Posted code is created based on one of the articles I found, I have remake it according to my needs but this does not work. Could anyone point me where I am doing mistake?

  • 写回答

1条回答 默认 最新

  • dptsivmg82908 2013-03-13 09:01
    关注

    You can't set php variable from javascript, this will not work

    var dropdownValue=document.getElementById("dropdown").value;
    $field=dropdownValue;
    ...
    <option value="st"><? echo $field; ?></option>
    

    you can do two things use ajax to generate other select or use pure javascript, using jQuery it will be something like:

    $('option[value=st]').text(dropdownValue);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置