2013-11-22 61 views
0

我试图建立一个优惠券系统到我的网站,我想用jQuery AJAX来做这件事。通过jQuery AJAX和关键事件搜索特定的术语

我那么如何正在试图做...

我在coupon.php与此数据阵列,它:

Array 
(
    [admins] => 50 
    [helpers] => 20 
) 

管理员是优惠券, 是用户将得到的销售。呈现html表单的用户当然需要输入优惠券名称。

这是我的jQuery的样子:

$(function() { 
    var ilength = 3; 

    var price_class = document.getElementsByClassName('price_no'); 
    var price_no = parseFloat(price_float[0].innerHTML); 

    $('#coupon_entry').keyup(function() { 
     var that = this, 
     value = $(this).val(); 

     if (value.length >= ilength) 
     { 
      $.ajax({ 
       url: 'coupon.php', 
       type: 'POST', 
       data: {'coupon': value}, 
       dataType: 'html', 

       success: function(result) { 
        if (result != 0) 
        { 
         $('.price_no').text(result/100 * price_no); 
        } 
       } 
      }); 
     } 
    }); 
}); 

这是怎么.price_no看起来像HTML:

<span class="price">Total: <strong><span class="price_no">28.9</span> USD</strong></span> 

这就是我如何处理整件事在PHP:

$coupon_post  = $_POST['coupon']; 
$coupons_str_ary = explode(';', $config['mp_coupons']); 
$coupons_array = array(); 

foreach ($coupons_str_ary as $coupon) 
{ 
    $coupon_percentage = substr(strrchr($coupon, '='), 1); 
    $coupon_name = $market->reverse_strrchr($coupon, '=', 0); 

    $coupons_array[$coupon_name] = $coupon_percentage; 
} 

unset($coupons_array[0]); 
echo (array_key_exists($coupon_post, $coupons_array)) ? (float) $coupons_array[$coupon_post] : 0; 
?> 

这个PHP脚本有我在上面发布的那个数组,然后我使用用户的输入来调用数组的键,然后返回消息(echo)用于获得新的销售价格,如果使用优惠券。


问题

我上述工作都不做 - 数量没有变化的。

P.S:我也使用了jQuery text()方法。但.price_no将始终成为/返回NaN,然后我切换到经典的JavaScript来抢类名称。

我在做什么错?

回答

1
var price_class = document.getElementsByClassName('price_no'); 
var price_no = parseFloat(price_float[0].innerHTML); //error is here 

我认为你必须price_float改变[0]到price_class [0]

+0

我觉得瞎了。谢谢! – Aborted

1

改变你的代码做

var price_class = document.getElementsByClassName('price_no'); 
//variable name is price_class not price_float 
var price_no = parseFloat(price_class[0].innerHTML);