javascript无法正常工作 - $未定义问题

时间:2011-04-23 02:42:03

标签: javascript jquery

我整天都在争吵,在google和stackoverflow上搜索但是无法理解为什么这样做有效:

<head>

    <script src="/js/jquery.js" type="text/javascript"></script>

       <script type="text/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>

这适用于jsFiddle而不是我的网站或JSbin,女巫给出了错误:

  

$未定义由行引起   (28/26):   $( '输入:无线​​电[名称= “RADIOSELECTOR”]')改变(()的函数。   {

还有一种方法,当“选择”被禁用时,它会自动转到第一个选项吗?提前谢谢

3 个答案:

答案 0 :(得分:3)

该行

<script src="js/jquery.js"></script>

不是正确的路径。尝试

<script src="/js/jquery.js"></script>

或使用

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>

小提琴有效,因为它为你加载了jQuery。

答案 1 :(得分:2)

@Rafal:看起来你错过了

$(document).ready(function() {
    // your code
)};

答案 2 :(得分:2)

在加载dom之前,你正在进行绑定。您需要将脚本包装在

$(document).ready(function() {
});

或同一事物的简写版

$(function() { 
});