无法从跨度获得价值

时间:2017-03-26 02:46:02

标签: javascript php jquery html

我有以下标记:

<div ref="fieldname35_1" class="cff-summary-item">
    <span class="summary-field-title cff-summary-title">Price </span>
    <span class="summary-field-value cff-summary-value">1650 </span>
</div>

试图通过以下方式获得价格:

$price = simple_basket_custom_fields($id, get_option('simple_basket_catalog_price'));

我在这里写simple_basket_custom_fields

<label for="priceCustomFiled">Price Custom Field</label>

完整代码:

<?php
/**
 * AJAX API 
 */

$simpeBasketAPI = new SimpleBasketAPI();


class SimpleBasketAPI
{
     private $basket;   



    public function __construct()
    {
        if ( is_admin() ) 
        {
            add_action( 'wp_ajax_nopriv_getTime', array( &$this, 'getTime'));
            add_action( 'wp_ajax_getTime', array( &$this, 'getTime'));

            add_action( 'wp_ajax_nopriv_getData', array( &$this, 'getData'));
            add_action( 'wp_ajax_getData', array( &$this, 'getData'));

            add_action( 'wp_ajax_nopriv_add', array( &$this, 'add'));
            add_action( 'wp_ajax_add', array( &$this, 'add'));



        }
        add_action( 'init', array( &$this, 'init' ) );


        $this->basket = SimpleBasketOrder::create();
    }

    public function init()
    {
        wp_enqueue_script('simple-basket', plugin_dir_url( __FILE__ ) . 'js/simple-basket.min.js', array( 'jquery' ) );
        wp_localize_script('simple-basket', 'SimpleBasket', array(
            'ajaxurl' => admin_url( 'admin-ajax.php' ),
            'nonce' => wp_create_nonce( 'ajax-example-nonce' )
        ) );
    }

    public function validateNonce()
    {
        if ( ! isset( $_REQUEST['nonce'] ) || ! wp_verify_nonce( $_REQUEST['nonce'], 'ajax-example-nonce' ) )
            die ( 'Invalid Nonce' );
    }

    public function responce($result)
    {
        header('Content-Type: application/json');
        echo json_encode($result);
        exit;
    }

    /* --------------------- AJAX---------------------- */
    public function getTime()
    {
        $this->validateNonce();
        $this->responce(array(
            'time' => date('d.m.Y H:i:s')
        ));
    }


    public function getData()
    {
        $this->validateNonce();
        $this->responce($this->basket);
    }


    public function add()
    {
        $this->validateNonce();
        if (!isset($_REQUEST['id']))
            die ( 'ID not specified' );


            $id = (int) $_REQUEST['id'];
            $product = get_post($id);
            $title = $product->post_title;
            $price = simple_basket_custom_fields($id, get_option('simple_basket_catalog_price'));
            $category = '';

            $postType = $product->post_type;

            $taxonomies = get_object_taxonomies($postType);
            foreach ($taxonomies as $taxonomy)
            {
                if (strpos($taxonomy, 'tag') !== FALSE) continue;               $categories = get_the_terms($id, $taxonomy);
                $category = (count($categories) > 0) ? $categories[0]->name : '';

                break;
            }


            if (!empty($title)) $this->basket->add($id, $title, $price, $category);
        $this->responce($this->basket);
    }
}

我尝试将$id更改为$span,但它无效。 :(

1 个答案:

答案 0 :(得分:0)

如果您想从span获取价值,请使用$(".cff-summary-value").text();

$( document ).ready(function() {  
var summaries = [];
summaries=$( ".summary-field-value.cff-summary-value" );

$.each(summaries, function( i, v ){
  alert($(".cff-summary-value.cff-summary-value:eq("+i+")").text());
});

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div ref="fieldname35_1" class="cff-summary-item">
    <span class="summary-field-title cff-summary-title">Price </span>
    <span class="summary-field-value cff-summary-value">1650 </span>
</div>
 <div ref="fieldname35_1" class="cff-summary-item">
    <span class="summary-field-title cff-summary-title">Price </span>
    <span class="summary-field-value cff-summary-value">1800 </span>
</div>

参考.text()