javascript - jQuery 在 HTML 文档加载后自动执行一个事件;

标签 javascript jquery

我想在文档的ready()上执行onchange事件。

原函数为onchange=“fucntionname(parms)”

<select name="country" id="selCountries" onchange="region.changed(this, 1, 'selProvinces')">
    <option value="0">{$lang.please_select}{$name_of_region[0]}</option>
    <!-- {foreach from=$country_list item=country} -->
    <option value="{$country.region_id}" {if $consignee.country eq $country.region_id}selected{/if}>{$country.region_name}</option>
    <!-- {/foreach} -->
</select>

Javascript

<script type="text/javascript">
    $(document).ready(function(){
        var onll=document.getElementsById('selProvinces');
        region.changed(onll, 1, 'selProvinces');
    })
</script>

我收到错误

document.getElementsById is not a function at HTMLDocument.

最佳答案

正如所讨论的,getElementById 中存在拼写错误 - 但更进一步 - 因为您使用的是 jQuery - 您可以将 html 和事件处理程序分开以提供更清晰的代码。将结构和功能分开总是更好。

<select name="country" id="selCountries">
    <option value="0">{$lang.please_select}{$name_of_region[0]}</option>
    <!-- {foreach from=$country_list item=country} -->
    <option value="{$country.region_id}" {if $consignee.country eq $country.region_id}selected{/if}>{$country.region_name}</option>
    <!-- {/foreach} -->
</select>


//Javascript
<script>
    $(document).ready(function(){
        $('#selCountries').change(function(){
        var region=$(this).val();
        region.changed(region, 1, 'selProvinces');
       })
    })
</script>

关于javascript - jQuery 在 HTML 文档加载后自动执行一个事件;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44261564/

相关文章:

jQuery td onclick 设置复选框,不冒泡

javascript - jquery ajax 从容器内的按钮加载内容

javascript - $().html()后无法进入空白表单textarea;

javascript - 在 Web 浏览器中创建可点击的网格

JavaScript/Angular 触发器表单验证

javascript - 新的日期(日期).getTime();以 d/m/y 格式读取时间

javascript - 关于 javascript/jquery 的问题

javascript - Jquery 检测元素何时通过滚动运动完全隐藏

javascript - Base 10 到 Base 2 转换器

javascript - 如何在事件处理程序中过滤掉子元素事件?