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 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致