jquery不工作

时间:2011-04-28 06:54:03

标签: php jquery

我正在使用jquery和ajax。有时,它会给出错误$未定义。

这是我的代码:

<script language="javascript" type="text/javascript">
  var base_path="<? echo $this->baseUrl().'/'; ?>";

    function checkemail()
    {
                var email=document.getElementById('comp_email').value;
                //alert(email);
        if(email!='')
        {

          $.ajax({
                                 url:  base_path + "admin/jobs/emailcheck/",
                                data: "email="+email,
                                type: 'GET',
                         success: function (resp) 
                         {

                                         //alert(resp);

                                        $("#valid_email").html(resp);

                                        //alert($("#valid_email table tr td").text());
                                        if($("#valid_email table tr td").text()=='This firm is not exist in database. Firstly register this firm.')
                                        {
                                                //alert('ok');
                                                $.ajax({
                                                         url:  base_path + "admin/profile/firmadd/",
                                                        data:"flag=jobsController",
                                                        type: 'POST',
                                                 success: function (resp) 
                                                 {
                                                        document.getElementById('firm_add_form').style.display='';
                                                        //alert(resp);
                                                                $("#firm_add_form").html(resp);
                                                                document.getElementById('register_typ').focus();
                                                                return false;
                                                 },
                                                        error: function(e){  alert('Error: '+e);  }  
                                                });                                     
                                        }
                                        else
                                        {
                                                document.getElementById('firm_add_form').style.display='none';
                                                document.getElementById('valid_email').style.display='none';
                                                var comp_name = resp.substring(0,resp.indexOf(',',0));
                                                var comp_web = resp.substring(resp.indexOf(',',0)+1);
                                                document.getElementById('comp_name').value = comp_name;
                                                document.getElementById('comp_web').value = comp_web;
                                                return true;
                                        }
                         },
                                error: function(xhr ,e){ 
                                        alert(xhr.readyState); 
                                        alert('Error: '+e);  }  
                        });
                }
  }
 </script>

此功能在文本框中的onblur上调用,并在相同表单的提交按钮上调用onclick

但是当我在onblur上调用它时,它正常工作,但是当我在onclick上调用它时,它会给出错误$ is not defined

当我发出提醒(电子邮件)时,它在提交按钮上的onclick上正常工作。

我的表格如下:

        <form id="addfrm" enctype="application/x-www-form-urlencoded" method="post" action=""
  name="">
    <table align="center" class="tbcss" width="100%" cellspacing="0" cellpadding="0">
      <tr>
        <td align="right" valign="top" width="15%"><label for="comp_email" class=
        "optional">Company Email:</label></td>

        <td class="element" width="26%" valign="top"><input type="text" name="comp_email"
        id="comp_email" value="" size="35" onblur="return checkemail()"><span id=
        "valid_email" align="left" width="55%"></span></td>

        <td align="right" valign="top" width="15%"><label for="comp_name" class=
        "required">Company Name:</label></td>

        <td class="element" width="26%" valign="top"><input type="text" name="comp_name"
        id="comp_name" value="" size="35"></td>
      </tr>

      <tr>
        <td id="firm_add_form" colspan="4"></td>
      </tr>

      <tr>
        <td align="right" valign="top" width="15%"><label for="comp_web" class=
        "optional">Company Website:</label></td>

        <td class="element" width="26%" valign="top"><input type="text" name="comp_web"
        id="comp_web" value="" size="35"></td>

        <td align="right" valign="top" width="15%"><label for="category" class=
        "required">Job Category:</label></td>

        <td class="element" width="26%"><select name="category" id="category">
          <option value="" label="Select Category">
            Select Category
          </option>

          <option value="Architecture" label="Architecture">
            Architecture
          </option>

          <option value="Manufacturing" label="Manufacturing">
            Manufacturing
          </option>

          <option value="IT" label="IT">
            IT
          </option>
        </select></td>
      </tr>

      <tr>
        <td align="right" valign="top" width="15%"><label for="job_title" class=
        "optional">Job Title:</label></td>

        <td class="element" width="26%" valign="top"><input type="text" name="job_title"
        id="job_title" value="" size="35"></td>

        <td align="right" valign="top" width="15%"><label for="contact_prsn" class=
        "optional">Contact Person:</label></td>

        <td class="element" width="26%" valign="top"><input type="text" name=
        "contact_prsn" id="contact_prsn" value="" size="35"></td>
      </tr>

      <tr>
        <td align="right" width="15%"><label for="close_date" class="optional">Closing
        Date:</label></td>

        <td class="element" width="26%"><select name="close_date" id="close_date">
          <option value="" label="Date">
            Date
          </option>

          <option value="1" label="01">
            01
          </option>

          <option value="2" label="02">
            02
          </option>

          <option value="3" label="03">
            03
          </option>

          <option value="4" label="04">
            04
          </option>

          <option value="5" label="05">
            05
          </option>

          <option value="6" label="06">
            06
          </option>

          <option value="7" label="07">
            07
          </option>

          <option value="8" label="08">
            08
          </option>

          <option value="9" label="09">
            09
          </option>

          <option value="10" label="10">
            10
          </option>

          <option value="11" label="11">
            11
          </option>

          <option value="12" label="12">
            12
          </option>

          <option value="13" label="13">
            13
          </option>

          <option value="14" label="14">
            14
          </option>

          <option value="15" label="15">
            15
          </option>

          <option value="16" label="16">
            16
          </option>

          <option value="17" label="17">
            17
          </option>

          <option value="18" label="18">
            18
          </option>

          <option value="19" label="19">
            19
          </option>

          <option value="20" label="20">
            20
          </option>

          <option value="21" label="21">
            21
          </option>

          <option value="22" label="22">
            22
          </option>

          <option value="23" label="23">
            23
          </option>

          <option value="24" label="24">
            24
          </option>

          <option value="25" label="25">
            25
          </option>

          <option value="26" label="26">
            26
          </option>

          <option value="27" label="27">
            27
          </option>

          <option value="28" label="28">
            28
          </option>

          <option value="29" label="29">
            29
          </option>

          <option value="30" label="30">
            30
          </option>

          <option value="31" label="31">
            31
          </option>
        </select> <span class="element"><select name="close_month" id="close_month">
          <option value="" label="Month">
            Month
          </option>

          <option value="01" label="Jan">
            Jan
          </option>

          <option value="02" label="Feb">
            Feb
          </option>

          <option value="03" label="Mar">
            Mar
          </option>

          <option value="04" label="Apr">
            Apr
          </option>

          <option value="05" label="May">
            May
          </option>

          <option value="06" label="Jun">
            Jun
          </option>

          <option value="07" label="Jul">
            Jul
          </option>

          <option value="08" label="Aug">
            Aug
          </option>

          <option value="09" label="Spt">
            Spt
          </option>

          <option value="10" label="Oct">
            Oct
          </option>

          <option value="11" label="Nov">
            Nov
          </option>

          <option value="12" label="Dec">
            Dec
          </option>
        </select></span> <span class="element"><select name="close_year" id="close_year">
          <option value="" label="Year">
            Year
          </option>

          <option value="2009" label="2009">
            2009
          </option>

          <option value="2010" label="2010">
            2010
          </option>

          <option value="2011" label="2011">
            2011
          </option>

          <option value="2012" label="2012">
            2012
          </option>

          <option value="2013" label="2013">
            2013
          </option>

          <option value="2014" label="2014">
            2014
          </option>

          <option value="2015" label="2015">
            2015
          </option>

          <option value="2016" label="2016">
            2016
          </option>

          <option value="2017" label="2017">
            2017
          </option>

          <option value="2018" label="2018">
            2018
          </option>

          <option value="2019" label="2019">
            2019
          </option>

          <option value="2020" label="2020">
            2020
          </option>

          <option value="2021" label="2021">
            2021
          </option>
        </select></span></td>

        <td align="right" valign="top" width="15%"><label for="location" class=
        "required">Location:</label></td>

        <td class="element" width="26%"><input type="text" name="location" id="location"
        value="" size="35"></td>
      </tr>

      <tr>
        <td align="right" valign="top" width="15%"><label for="job_detail" class=
        "optional">Job Details:</label></td>

        <td class="element" width="26%" valign="top">
        <textarea name="job_detail" id="job_detail" cols="30" rows="5">
</textarea></td>

        <td align="right" valign="top" width="15%"><label for="notes" class=
        "required">Notes:</label></td>

        <td class="element" width="26%">
        <textarea name="notes" id="notes" cols="30" rows="5">
</textarea></td>
      </tr>

      <tr>
        <td align="right" valign="top" width="15%"><label for="block" class=
        "optional">Block:</label></td>

        <td class="element" width="26%"><input type="hidden" name="block" value=
        "N"><input type="checkbox" name="block" id="block" value="Y"></td>

        <td align="right" valign="top" width="15%"><label for="source" class=
        "optional">Job Source/Link:</label></td>

        <td class="element" width="26%" valign="top"><input type="text" name="source" id=
        "source" value=""></td>
      </tr>

      <tr>
        <td align="center" colspan="4"><input type="submit" name="add" id="add" value=
        "Add" onclick="return checkemail();"> <button name="back" id="back" type="button"
        onclick="window.location='/allindiazend/admin/jobs/list'">Back</button></td>
      </tr>
    </table>
  </form>

任何身体都可以帮助我。

3 个答案:

答案 0 :(得分:5)

$由jQuery库定义。因此,您可能不会在您的页面上成功包含该库。

  • 检查您是否包含<script>元素并且不是404错误。
  • 检查您是否在页面中执行了早期,然后尝试使用它
  • 检查您是否{{1}了脚本请求
  • 检查浏览器是否正在发出请求(在您使用的任何调试工具(例如Firebug)的网络选项卡中)

答案 1 :(得分:0)

有时你的jQuery没有正确加载。理想情况下,使用Google的CDN加载它。

答案 2 :(得分:-2)

您应该将所有jQuery的脚本放在文档就绪块中:

<script type="text/javascript">
    $(document).ready(function(){
            //call jQuery enabled functions here
        });
</script>

这确保在执行

之前加载jQuery(并定义$)