我想在文档的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/