Javascript 无法从span获取值

Javascript 无法从span获取值,javascript,php,jquery,html,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> 我正在这里编写简

我的标记如下:

<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>
我正在这里编写
简单的\u篮子\u自定义\u字段

<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);
    }
}

如果要从span中获取值,请使用
$(“.cff summary value”).text()

$(文档).ready(函数(){
var汇总=[];
摘要=$(“.summary字段值.cff摘要值”);
各美元(摘要、职能(i、v){
警报($(“.cff摘要值.cff摘要值:eq(“+i+”)).text());
});
});

价格
1650
价格
1800

当您编写
$id
时,是否可以更新函数“simple\u basket\u custom\u fields”的代码,因为标记中没有任何id,所以没有意义。另外,
$price
是一个变量吗?请上传完整的代码,比如@user93。我认为更新设置是可以的/这是Java还是JavaScript?Java和JavaScript类似,就像汽车和地毯一样。回答时间:2008年10月28日22:12 Greg Hewgill由于php无法获得span($price)的值,因此问题没有得到解决
<?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);
    }
}