事件仅在首次单击后触发

时间:2017-04-27 07:07:19

标签: javascript php jquery ajax

我有一份类别列表。当我第一次发送请求时,单击某个类别以触发AJAX请求。当我第二次点击它很棒。这是我的代码

<?php foreach($this->category as $value) { ?>
  <li data-cid=<?= $value['cat_id']?> class="cid">
    <a href="http://localhost/mvc/viewbycat/index/<?= $value['cat_id'] ?>">
      <?= $value['cat_name']?>
    </a>
  </li>
<?php } ?>
$(document).ready(function () {
  var catid;
  var count = 0;

  $('li.cid').click(function (e) {
    count = 0;
    var limit = 6;
    var offset = 0;
    e.preventDefault();
    $("#main").empty();
    catid = $(this).attr('data-cid');
    displayRecords1(limit, offset, catid);
  });

感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

您遇到Unexpected end of input错误,因为您遗漏了});

  $(document).ready(function() {
  var catid,
      count = 0;
  $('li.cid').click(function(e) {
    e.preventDefault();

    var limit = 6,
        offset = 0,
        count = 0;

    $("#main").empty();

    catid = $(this).attr('data-cid');

    displayRecords1(limit, offset, catid);
  });
});

更改,

<li data-cid=<?= $value['cat_id']?> class="cid">

/** Note the double quotes. **/
<li data-cid="<?= $value['cat_id']?>" class="cid">
  • 避免在PHP中使用短标记,因为它们可能在您的服务器上被禁用。
  • 请务必检查控制台是否存在JavaScript错误。
  • 在开始时尝试并初始化所有变量以防止混淆。

阅读材料

Opening Console in Different Browsers

PHP Short Tags

JavaScript Syntax Checker - 快速检查语法。

答案 1 :(得分:0)

您没有关闭$(document).ready(function () { var catid; var count=0; $('li.cid').click(function (e) { count=0; var limit= 6; var offset = 0; e.preventDefault(); $("#main").empty(); catid = $(this).attr('data-cid'); displayRecords1(limit, offset, catid); }); }); // This here

public class StepCounterService extends Service {
    private static final String LOG_TAG = "ForegroundService";

    public static Boolean FLAG = false;

    private SensorManager mSensorManager;
    private StepDetector detector;

    private PowerManager mPowerManager;
    private WakeLock mWakeLock;


    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        startServiceForeground(intent, flags, startId);


        return START_STICKY;
    }

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        new StepCountManager(this);


        FLAG = true;

        Log.e("Service_Started", "");
        detector = new StepDetector(this);


        mSensorManager = (SensorManager) this.getSystemService(SENSOR_SERVICE);

        mSensorManager.registerListener(detector,
                mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
                SensorManager.SENSOR_DELAY_NORMAL);


        mPowerManager = (PowerManager) this
                .getSystemService(Context.POWER_SERVICE);
        mWakeLock = mPowerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK
                | PowerManager.ACQUIRE_CAUSES_WAKEUP, "S");
        mWakeLock.acquire();
        reloadSettings();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        FLAG = false;
        if (detector != null) {
            mSensorManager.unregisterListener(detector);
        }

        if (mWakeLock != null) {
            mWakeLock.release();
        }

        Log.e("Service_destroyed", "");
    }

    public void reloadSettings() {

        if (detector != null) {
            detector.setSensitivity(
                    Float.valueOf("10")
            );
        }


    }

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        super.onTaskRemoved(rootIntent);
        Intent restartService = new Intent(getApplicationContext(),
                this.getClass());
        restartService.setPackage(getPackageName());
        PendingIntent restartServicePI = PendingIntent.getService(
                getApplicationContext(), 1, restartService,
                PendingIntent.FLAG_ONE_SHOT);

        //Restart the service once it has been killed android

        ((AlarmManager) getSystemService(Context.ALARM_SERVICE))
                .set(AlarmManager.RTC, System.currentTimeMillis() + 1000, PendingIntent
                        .getService(this, 3, new Intent(this, StepCounterService.class), 0));

    }

    public int startServiceForeground(Intent intent, int flags, int startId) {
        Intent notificationIntent = new Intent(this, HomeActivity.class);
        notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP| Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Mobiefit Walk")
                .setContentIntent(pendingIntent)
                .setOngoing(true)
                .build();
        NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager.notify(300, notification);
        Notification n;

        startForeground(300, notification);

        return START_STICKY;
    }


}