jquery代码不起作用
美好的一天,
我在html页面上使用jquery脚本时遇到了问题。
该脚本在jsfiddle上运行正常
在我的页面上我用过:
Pickup location:
Name on meeting plate:
Your phone number:
Time of pickup:
Pickup location:
Where to pickup Airport Railway station Address Dropoff location:
Where to dropoff Airport Railway station Address
但是在页面加载时它不起作用。
你必须将bookform.js
的代码bookform.js
在DOM就绪处理程序中,就像:
$(document).ready(function() { $('#select1').change(function(){ if($(this).val() == 'pick_airport'){ $('#pick_nextStep').html('
Flight:
'); } if($(this).val() == 'pick_railway'){ $('#pick_nextStep').html('
Wagon:
'); } if($(this).val() == 'pick_address'){ $('#pick_nextStep').html('Pickup address:
'); } }); $('#select2').change(function(){ if($(this).val() == 'drop_airport'){ $('#drop_nextStep').html('
'); } if($(this).val() == 'drop_railway'){ $('#drop_nextStep').html('') } if($(this).val() == 'drop_address'){ $('#drop_nextStep').html('Your destination address:
'); } }); });
尝试将代码包装在DOM就绪处理程序$(document).ready(function() {....})
或更短的forms$(function() {....})
,以确保所有DOM元素都已加载在执行jQuery代码之前正确执行。
$(function() { // Your code here });
在页面的head标签中添加jquery库的脚本标签。如下所示:
可能问题是你的脚本在加载所有DOM元素之后加载。
你需要反过来。 上面有两个答案:一个说在容器中包含
标记。
它可能有效,但脚本可能会在整个页面加载之前拍摄。 要防止这种情况发生,只需将所有jQuery代码包装到此函数中:
$(function(), { // your code here; });
它与$(document).ready()
等相同。
如果仍然无效,请尝试启用控制台(在Google Chrome中按F12并搜索错误)。