doupan6648 2012-07-18 13:10
浏览 877
已采纳

在页面加载时触发onchange html事件

Ok I have a onchange event on a select field. It now works great. When the dropdown "networks" is changed it refreshes the second dropdown. I also want the ajax code at the top to trigger on page load as well as onchange so the second list gets populated. This is due to it being on an edit page. Here is the ajax call im using first

function get_cities(networks) {
    $.ajax({
        type: "POST",
        url: "select.php", /* The country id will be sent to this file */
        beforeSend:     function () {
            $("#folder").html("<option>Loading ...</option>");
        },
        //data: "idnetworks="+networks,
        data: "idnetworks="+networks +"&doc="+ <?php echo $row_rs_doc['parentid']; ?>,
        success: function(msg){
            $("#folder").html(msg);
        }
    });
} 

now the two dropdown boxes

<label for="networks"></label>
<select name="networks" id="networks" onChange='get_cities($(this).val())'>
    <?php
    do {  
    ?>
        <option value="<?php echo $row_rs_net['idnetworks']?>"<?php if (!(strcmp($row_rs_net['idnetworks'], $row_rs_doc['network']))) {echo "selected=\"selected\"";} ?>><?php echo $row_rs_net['netname']?></option>
    <?php
    } while ($row_rs_net = mysql_fetch_assoc($rs_net));
    $rows = mysql_num_rows($rs_net);
    if($rows > 0) {
        mysql_data_seek($rs_net, 0);
        $row_rs_net = mysql_fetch_assoc($rs_net);
    };
    ?>
</select>
<select name="folder" id="folder">
</select>
  • 写回答

3条回答 默认 最新

  • duanhuang2150 2012-07-18 13:19
    关注

    You can use .trigger() to trigger a change event onto the select-box so the onchange code will be called like it would if the user just switched the option.

    jQuery('#networks').trigger('change');
    

    Just include this into the load event/function for the page.

    jQuery(document).ready(function() {
        jQuery('#networks').trigger('change');
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python