javascript not working - $ is not defined problem
Im fighting with this all day, searching on google and on stackoverflow but cant get it why is this working:
<head>
<script src="/js/jquery.js" type="text/javascript"></script>
<script type="t开发者_如何转开发ext/javascript">
$('input:radio[name="RADIOSELECTOR"]').change(function() {
if ($(this).val()=='one') {
$('#ones').removeAttr('disabled');
$('#twos').attr('disabled',true);
} else if ($(this).val()=='two') {
$('#ones').attr('disabled',true);
$('#twos').removeAttr('disabled');
} else {
$('#ones').attr('disabled',true);
$('#twos').attr('disabled',true);
}
});
</script>
</head>
<form name="order" action="managecart.html" onsubmit="return AddToCart(this); return ValidateCart(this);">
<input name="PRICE" type=hidden id="PRICE" value="" />
<input name=NAME type=hidden id="NAME" value="" />
<input name=ID_NUM type=hidden id="ID_NUM" value="" />
<input type=hidden name="SHIPPING" value="0.00" />
<input type="radio" name="RADIOSELECTOR" value="lose" onClick="document.order.PRICE.value='59.99'; document.order.NAME.value='iPhone 3G Glass/digitiazer replacement'; document.order.ID_NUM.value='3GGL'; document.order.SHIPPING.value='0.00';" />Glass/digitizer repair <b>59.99$</b><br><br>
<b> Combo repairs:</b><BR><br>
<input type="radio" name="RADIOSELECTOR" value="lose" onClick="document.order.PRICE.value='74.99'; document.order.NAME.value='iPhone 3G Glass/digitizer + battery repair'; document.order.ID_NUM.value='3GGLB'; document.order.SHIPPING.value='0.00';" />Glass/digitizer + battery repair = <b> 74.99$</b> 25$ in savings!<br>
<input type="radio" name="RADIOSELECTOR" value="one" onClick="document.order.PRICE.value='79.99'; document.order.NAME.value='iPhone 3G Glass/digitizer + combo '; document.order.ID_NUM.value='3GGLC'; document.order.SHIPPING.value='0.00';" />Glass/digitizer + <select id="ones" name="ADDITIONALINFO2" disabled="disabled">
<option value=" "></option><option value="Home button">Home button</option> <option value="Power button">Power button</option><option value="Mute switch">Mute switch</option><option value="volume button">Volume button</option></select>
= <b>79.99$</b> 30$ in savings!<br>
<input type="radio" name="RADIOSELECTOR" value="two" onClick="document.order.PRICE.value='99.99'; document.order.NAME.value='iPhone 3G Glass/digitizer + combo + ''ADDITIONALINFO3'; document.order.ID_NUM.value='3GGLBC'; document.order.SHIPPING.value='0.00';" />Glass/digitizer + battery + <select name="ADDITIONALINFO3" id="twos" disabled="disabled><option value=" "></option><option value="Home button">Home button</option> <option value="Power button">Power button</option><option value="Mute switch">Mute switch</option><option value="volume button">Volume button</option></select> = <b>99.99$</b> 30$ in savings!<br>
<input name="QUANTITY" type="hidden" value="1" /><br>
<input type="image" src="./images/addtocart.png" align="center" name="BUY" value=" Add to cart " ALT="Add to Cart">
</form>
this works on jsFiddle but not on my website or JSbin, witch gives error:
$ is not defined Caused by line (28/26): $('input:radio[name="RADIOSELECTOR"]').change(function() {
Also is there a way that when "select" gets disabled it automatically goes to first option? Thank you in advance
The line
<script src="js/jquery.js"></script>
is not the correct path. Try
<script src="/js/jquery.js"></script>
or use
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
The fiddle works because it loads jQuery for you.
You're doing your bindings before the dom is loaded. You need to wrap your script in
$(document).ready(function() {
});
or the shorthand version of the same thing
$(function() {
});
@Rafal: It looks like you're missing
$(document).ready(function() {
// your code
)};
精彩评论