将href更改为单选按钮的简便方法?

时间:2013-01-24 10:34:14

标签: php forms radio-button hyperlink

我在oscommerce中有一些代码,使客户能够通过点击文本链接查看包含增值税或增值税的价格。 我想将textlink更改为这样:

“显示价格[X]含增值税[]不含增值税” ([]是radiobuttons)

我尝试过在网上找到的一些不同的解决方案,但我还不够熟练。 我希望有人可以快速看一下并给我一个提示? =)

<?php echo
'  <div>' .
'    ' . ((DISPLAY_PRICE_WITH_TAX == 'true') ? '<strong>' . TEXT_DISPLAYING_PRICES_WITH_TAX . '</strong>' : '<a href="' . tep_href_link(FILENAME_DEFAULT, 'action=toggle_tax&display_tax=true&uri='. urlencode($_SERVER['REQUEST_URI'])) . '">' . TEXT_DISPLAY_PRICES_WITH_TAX . '</a>') . '' .
'    ' . ((DISPLAY_PRICE_WITH_TAX == 'false') ? '<strong>' . TEXT_DISPLAYING_PRICES_WITHOUT_TAX . '</strong>' : '<a href="' . tep_href_link(FILENAME_DEFAULT, 'action=toggle_tax&display_tax=false&uri='. urlencode($_SERVER['REQUEST_URI'])) . '">' . TEXT_DISPLAY_PRICES_WITHOUT_TAX . '</a>') . 
'  </div>'?>

1 个答案:

答案 0 :(得分:0)

如果您正在尝试执行我认为您尝试执行的操作,那么您希望允许用户显示有/无税的价格。

一种可能性是使用JavaScript库jQuery

如果您决定使用此路线,则可以将每个价格打印到页面,但隐藏用户不希望看到的价格。

<html>
<head>

<!-- Include the jQuery Library via CDN -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

</head>
<body>

<!-- Display Prices, hide one -->
<div class="with_vat">$xxx</div>
<div class="without_vat" style="display:none">$yyy</div>

<!-- Options -->
<input type="radio" name="vat_choice" value="1" checked />  Show Vat
<input type="radio" name="vat_choice" value="0" />  Exclude Vat

<!-- jQuery to Hide/Show the divs when radio is changed -->
<script>
$("input[name='vat_choice']").change(function(){

  // Get Value
  var vatChoice = $(this).val();

  if(vatChoice == 1){

    $('.with_vat').show();
    $('.without_vat').hide();

  }
  else{

    $('.with_vat').hide();
    $('.without_vat').show();

  }

});
</script>


</body>
</html>

您可以在此处查看代码:http://jsfiddle.net/PKh3y/

单击单选按钮时,您还可以通过重定向来获得所需的结果。下面的解决方案确实使用(一点点)PHP。

<html>
<head>

<!-- Include the jQuery Library via CDN -->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>

</head>
<body>

<!-- Display Prices depending on $_GET parameters -->
<?php if(!isset($_GET['without_vat'])): ?>
<div class="with_vat">$xxx</div>
<?php else: ?>
<div class="without_vat">$yyy</div>
<?php endif; ?>

<!-- Options -->
<input type="radio" name="vat_choice" value="1" checked />  Show Vat
<input type="radio" name="vat_choice" value="0" />  Exclude Vat

<!-- jQuery to redirect when radio is changed -->
<script>
$("input[name='vat_choice']").change(function(){

  // Get Value
  var vatChoice = $(this).val();

  if(vatChoice == 1){

     window.location = 'http://example.com/';

  }
  else{

    window.location = 'http://example.com/?without_vat=1';

  }

});
</script>


</body>
</html>

我希望能回答你的问题,祝你好运!

相关问题