JS (JQuery) - is not defined


JS (JQuery) - is not defined

实际上我不是js开发人员。在获取元素时遇到一些问题(select.value)我的浏览器说我:未捕获的引用错误:未定义获取价格

据我所知,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);
        }
    });
}

目录:

<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>

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,它就可以工作!

您是否在 html 中使用了最新的 jquery?这可能是由于冲突以及将获取价格JavaScript代码放在html的底部。

在代码中使用最新的 jquery 文件https://jquery.com/download/

这是因为浏览器找不到方法getprice()定义。

您确定,您已在页面中包含该 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>