2016-02-19 69 views
1

其實我不是js開發者。獲取元素有一些問題(select.value) 我的瀏覽器說:未捕獲ReferenceError:getprice未定義JS(jQuery) - 未定義

正如我所瞭解的,jquery可以通過id select.value獲取元素。我該如何解決它? 謝謝!

JS代碼:

function getprice() { 
    $.ajax({ 
     type: "POST", 
     url: "<?=Core_Config::$Link?>store/cart", 
     data: { 
      pay_type: $("#pay_type").val();, 
      delivery_type: $("#delivery_type").val();, 
      totalprice: "<?=$total?>" 
     }, 

     success: function(html) { 
      $("#content22").html(html); 
     } 
    }); 
} 

HTML:

<tr> 
    <td><span class="required">*</span> Delivery:</td> 
    <td> 
     <select name="address[delivery]" id="delivery_type"> 
     <? foreach ($this->db->query($this->delivery) as $delivery): ?> 
      <option value="<?=$delivery['id']?>"><?=$delivery['name']?> (<?=$delivery['cost']?> грн)</option> 
     <? endforeach; ?> 
     </select> 
    </td> 
</tr> 
<tr> 
    <td><span class="required">*</span> Payment:</td> 
    <td> 
     <select name="address[paytype]" id="pay_type" onchange="getprice();"> 
     <? foreach ($this->db->query($this->payType) as $paytype): ?> 
      <option value="<?=$paytype['id']?>"><?=$paytype['name']?> (<?=$paytype['cost']?> грн)</option> 
     <? endforeach; ?> 
    </select> 
    </td> 
</tr> 
+0

你確定你用用getPrice功能,本網站中加入腳本? –

+0

@PiotrCzarnecki javascript總是暴露給客戶端的用戶(現在我們不要談論節點),所以我沒有反對意見 –

+0

@Thomas - 我不明白你的意思,基本上我只是問是否包含這個腳本。也許它沒有被包含在HTML或捆綁包等 –

回答

0

function getprice() { 
    $.ajax({ 
     type: "POST", 
     url: "<?=Core_Config::$Link?>store/cart", 
     data: { 
      pay_type: $("#pay_type").val(), //<--removed ; 
      delivery_type: $("#delivery_type").val(), //<--removed ; also 
      totalprice: "<?=$total?>" 
     }, 

     success: function(html) { 
      $("#content22").html(html); 
     } 
    }); 
} 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
<tr> 
    <td><span class="required">*</span> Delivery:</td> 
    <td><select name="address[delivery]" id="delivery_type"> 
     <? foreach ($this->db->query($this->delivery) as $delivery): ?> 
      <option value="<?=$delivery['id']?>"><?=$delivery['name']?> (<?=$delivery['cost']?> грн)</option> 
     <? endforeach; ?> 
     </select> </td> 
</tr> 
<tr> 
    <td><span class="required">*</span> Payment:</td> 
    <td><select name="address[paytype]" id="pay_type" onchange="getprice();"> 
      <? foreach ($this->db->query($this->payType) as $paytype): ?> 
        <option value="<?=$paytype['id']?>"><?=$paytype['name']?> (<?=$paytype['cost']?> грн)</option> 
          <? endforeach; ?> 
       </select> </td> 
       </tr> 

它的工作原理,如果你包括jQuery的!

+0

只是錯誤使用';'現在是工作。謝謝! –

0

你用過最新的jquery html嗎?可能這是由於衝突,以及HTML的底部得到價格的JavaScript代碼。在你的代碼 https://jquery.com/download/

0

這是因爲瀏覽器無法找到該方法用getPrice()的定義

使用最新的jQuery文件。

你確定,你已經在頁面中包含getprice()函數定義。如果沒有,請保留該功能在文件的底部<script>標籤內:

<script type="text/javascript"> 
function getprice() { 
    $.ajax({ 
     type: "POST", 
     url: "<?=Core_Config::$Link?>store/cart", 
     data: { 
      pay_type: $("#pay_type").val();, 
      delivery_type: $("#delivery_type").val();, 
      totalprice: "<?=$total?>" 
     }, 

     success: function(html) { 
      $("#content22").html(html); 
     } 
    }); 
} 
<script>