如何使用php

时间:2015-06-13 10:54:30

标签: javascript php

我想在点击按钮上显示优惠券代码

  <script type='text/javascript' >
   jQuery('#id1').click(function(){
   jQuery(this).replaceWith("<?php echo $_coupon?>");
  })
</script> 

<?php if ($_coupon != '' ):?>
 <button   id="id1" type="button" class="but" value="Button Name">     </button>
<?php endif; ?>

这是我的代码,它仅适用于第一次按键点击,这意味着只显示一个值不能用作循环请帮我解决我的问题

3 个答案:

答案 0 :(得分:1)

我会这样做:

<script type='text/javascript' >
   jQuery('#id1').click(function(){
      jQuery(this).replaceWith(jQuery(this).val());
   })
</script> 

<?php if ($_coupon != '' ):?>
  <button id="id1" type="button" class="but" value="<?php echo $_coupon ?>"></button>
<?php endif; ?>

答案 1 :(得分:0)

我会添加一个隐藏的输入:

<?php if ($_coupon != '' ):?>
    <input type="hidden" id="coupon" value="<?php echo $_coupon ?>" />
    <button id="id1" type="button" class="but" value="Button Name"></button>
<?php endif; ?>

然后在jquery中获取对象的值:

  <script type='text/javascript' >
       jQuery('#id1').click(function(){
          jQuery(this).replaceWith($('#coupon').val());
       })
  </script>

答案 2 :(得分:0)

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/txtLabel"
             android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:textSize="16dp"
            android:text="Home View"/>


        <ImageView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/txtLabel"
            android:src="@drawable/ic_home"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="10dp"/>
        <Button
         android:id="@+id/btn"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_marginTop="40dip"
         android:background="@color/btn"
         android:text="@string/btn"
         android:textAllCaps="false"
         android:textColor="@color/white"
         android:textSize="15dp"/>
    </RelativeLayout>

它正常工作。 检查按钮的ID是否已在此页面的不同位置定义。因为id必须是唯一的。

也试试这个,

<script type='text/javascript' >
   jQuery('#id1').click(function(){
   jQuery(this).replaceWith("<?php echo $_coupon; ?>");
  });
</script> 

<?php if ($_coupon != '' ):?>
 <button   id="id1" class="but" value="Button Name"></button>
<?php endif; ?>

还检查是否包含jquery库