Py学习  »  msg  »  全部回复
回复总数  4
7 年前
回复了 msg 创建的主题 » 使用ajax的Django

你的 .cart-unit-price

假设您将此行模板作为变量中的字符串 tpl .

// Turn it into a jQuery element and return a copy
$tpl = $(tpl).clone();
// Now do your selection restricting scope to the tpl element
$('.cart-unit-price', $tpl).text(data.price);
// Alternative syntax
$tpl.find('.cart-unit-price').text(data.price);
// ... Other fields
// When all your templating is done, append to the table
$tpl.wrap('<tr></tr>').prependTo('table tbody');

如果您只想更新特定产品的金额,则应执行以下操作:

$(document).ready(function() {
      var cartAdd = $('.add-to-cart')
      cartAdd.click(function(event) {
            event.preventDefault();
            // The button that was clicked
            var thisLink = $(this)
            var hrefEndpoint = thisLink.attr('data-endpoint')
            var addCartMethod = "GET"
            var linkData = thisLink.serialize();
            // Find the enclosing .cart-data element
            var cartData = thisLink.closest('.cart-data')
            // And now find the corresponding quantity container inside
            var cartQuantity = cartData.find('.cart-quantity')

            console.log(cartQuantity)

            $.ajax({
              url: hrefEndpoint,
              method: addCartMethod,
              data: linkData,
              success: function(data) {
                // Set the new quantity
                cartQuantity.text(data.quantity)
                var currentPath = window.location.href
                if (window.location.href.indexOf('cart') != -1) {
                  updateCart()
                }
              },
              error: function(errorData) {
                console.log('error')
                console.log('error', errorData)
              }
            })
7 年前
回复了 msg 创建的主题 » 为什么这个javascript/jquery没有捕获用户输入?

当您 事实上 单击按钮以便获取值。

var baseURL = "http://api.giphy.com/v1/gifs/search?q="
var apiKey = "Yz4pO4lJDaMYGIX80M9gc2Mq7HKKS2or"

$("#find-giphy").on("click", function() {
  var input = $("#search-field").val();
  var queryURL = baseURL + input + "&api_key=" + apiKey + "&limit=10";
  $.ajax({
    url: queryURL,
    method: "GET"
  }).then(function(response) {
    console.log(response);
    console.log(queryURL);
  })
})
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0/css/bootstrap.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

<div class="container">
  <h1>gifTastic!</h1>
  <label for="search-field">Find a TV Show: </label>
  <input type="text" id="search-field">
  <input id="find-giphy" type="submit" value="giphy Search">
</div>
7 年前
回复了 msg 创建的主题 » 如何使用ajax从php推送到jquery数组?

在php中,您将坐标作为字符串输出,但在javascript中将它们作为json处理。您必须将坐标推入数组并对其进行编码:

if( $query->have_posts() ): 
    $coordinates = [];
    while( $query->have_posts() ) : $query->the_post();
        $location = get_field('location');
        $lat = $location['lat'];
        $lng = $location['lng'];
        $coordinates[] = $lat.", ".$lng;
    endwhile;
    echo json_encode($coordinates);
    die;
endif;
8 年前
回复了 msg 创建的主题 » 使用jquery根据主菜单的类名隐藏主菜单的<li>元素

打电话 hide 不是个好主意。它只更改项的可见性,但它仍将存在于源中。如果你 绝对地 必须在javascript中完成,调用 remove ,但此方法仍然不适用于禁用它的用户。

但幸运的是,在生成HTML之前,您可以通过挂钩从WordPress菜单中删除元素:

 function filter_remove_menu_item_for_anons($menu_items, $args) {
    foreach($menu_items as $k => $item) {
      if ($item->ID == 123) {
        unset($menu_items[$k]);
      }
    }
    return $menu_items;
  }
  add_filter('wp_nav_menu_objects', 'filter_remove_menu_item_for_anons', 10, 2);

只需根据会话检查注册过滤器。

如果您有多个菜单并且需要更好的控制,可以使用 args 参数。

但是,如果用户知道URL,他仍然可以访问内容,您应该考虑其他 solution .