简体   繁体   English

单击时禁用按钮

[英]Disable button when clicked

As you can see in the code below, I have 2 flags my and hk .正如您在下面的代码中看到的,我有 2 个标志myhk
When I click on the hk flag, it will redirect to the hk page and at the same time will disable the hk button to show the difference from other buttons.当我点击hk标志时,它将重定向到hk页面,同时将禁用hk按钮以显示与其他按钮的区别。

So,what php code do I use to disable only the button that is clicked?那么,我使用什么 php 代码来仅禁用被点击的按钮?

<button id="btn_my" dojoType="dijit.form.Button" 
  onClick='location = "<?php echo url_realurl();?>/results/view/all/M"'>
<?php 
  echo ci_create_img('my');
?> <!--this is malaysia flay image -->
</button>
<button id="btn_hk" dojoType="dijit.form.Button" 
  onClick='location = "<?php echo url_realurl();?>/results/view/all/H"'>
<?php 
  echo ci_create_img('hk');
?><!--this is hongkong flay image-->
</button>

You could read the $_SERVER['REQUEST_URI'] and disable the button if it links to the current page.您可以阅读$_SERVER['REQUEST_URI']并在按钮链接到当前页面时禁用该按钮。 Example:例子:

<?php
$countries = array(
    'my'    =>  '/results/view/all/M',
    'hk'    =>  '/results/view/all/H',
);
?>

<?php foreach ($countries as $code => $uri): ?>

    <?php if ($_SERVER['REQUEST_URI'] === $uri): ?>
    <button class="disabled">
    <?php else: ?>
    <button onClick='location="<?php echo url_realurl().$uri; ?>"'>
    <?php endif; ?>
        <?php echo ci_create_img($code);?>
    </button>

<?php endforeach; ?>

You might want to trim($uri, '/') and trim($_SERVER['REQUEST_URI'], '/') there to make sure there are no mistakes with slashes.您可能想在trim($uri, '/')trim($_SERVER['REQUEST_URI'], '/')以确保斜杠没有错误。

You could also append a $_GET variable to the link like ?country_code=hk and read that instead:您也可以 append 将$_GET变量指向链接,如?country_code=hk并改为阅读:

<?php foreach ($countries as $code => $uri): ?>

    <?php if ($_GET['country_code'] === $code): ?>
    <button class="disabled">
    <?php else: ?>
    <button onClick='location="<?php echo url_realurl().$uri.'?country_code='.$code; ?>"'>
    <?php endif; ?>
        <?php echo ci_create_img($code);?>
    </button>

<?php endforeach; ?>

There, you should also make sure to check if isset($_GET['country_code']) , and any other links on the page would most likely have to use the query string as well.在那里,您还应该确保检查isset($_GET['country_code'])以及页面上的任何其他链接很可能也必须使用查询字符串。 I was trying to make this readable.我试图让这个可读。 I would just write a function for reading the url personally.我只想写一个 function 来亲自阅读 url。

I prefer the first approach.我更喜欢第一种方法。 Sorry, I had to craft it in my coding style, but hopefully you get the idea and this works for you.抱歉,我不得不以我的编码风格来制作它,但希望你能明白这个想法,这对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM