根据所选选项更改表数据 [英] change the table data based on the selected option

查看:48
本文介绍了根据所选选项更改表数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表,显示数据库中的数据.我有一个选择框,该人员可以在其中选择要用于显示数据的货币.

I have a table displaying data from the database. I have a select box where the person can select the currency in which he wants the data to be displayed.

当他这样做时,我需要刷新数据并将其显示为新货币.而且,如果不刷新整个页面,我想不通.

When he does so, i need the data to be refreshed and displayed as the new currency. And I can't figure out how to do so without refreshing the whole page.

请问有什么想法吗?

<select id="currency">
<option value = "rub">В рублях</option>
<option value = "usd">USD</option>
<option value = "eur">EURO</option>
</select>
<table id="servers" cellpadding="0" cellspacing="0">
 <tr>
<td width="400"><b>Сервер</b></td>
<td width="100"><b>1 кк адены</b></td>
<td width="100"><b>1 ккк адены</b></td>
</tr>
<?php 
$sql = mysql_query("SELECT * FROM `prices`");
while($r = mysql_fetch_array($sql)) {
echo("\t\t<tr>\n");
echo("\t\t\t<td>".$r['name']."</td>\n");
echo("\t\t\t<td>$".round($r['kk']/$dollar, 2)."</td>\n");
echo("\t\t\t<td>$".round($r['kkk']/$dollar, 2)."</td>\n");
echo("\t\t</tr>\n");
} ?>
</table>

推荐答案

如果您可以保存数据转换的要素(用于转换USD> AUS USD> EUR或其他任何货币),则可以使用一个常规选择器在整个表中委派更改

If you can hold factors for data transformation (for conversion USD>AUS USD>EUR or whatever) you can delegate the change throughout the table by using one general selector.

首先,您将所有因素放置在js变量中,如下所示(这必须是php文件中的doe,并且其他内容可以在单独的js中完成):

first you place all the factors in the js vars like this(this must be doe inside php file and other stuff can be done in separated js):

var USDtoEUR = <?php echo $php2eur; ?>

接下来要做的是,绑定事件以单击选择框,按钮或其他任何东西,假设它是ID为"currency"的选择:

Next thing you do, you bind event to click on the select box, button or whatever, let's say it is select with id "currency":

$('#currency'). click(function() {
 switch ($('this').val()) {
   case 'EUR' {
      //Code we will discuss is going here
   }
   break;
//... etc...

因此,我们想更改所有假设的数据,它们都是按点击美元计算的,因此我们具有将美元转换为存储在单独的变量USDtoXXX中的其他值的所有因素.请注意,我们也需要一个USDtoUSD.

So, we want to change all the data we are assuming is in USD on click, so we have all the factors to convert from usd to other values stored in separate vars USDtoXXX. Note that we need one USDtoUSD, too.

现在...无论结构如何,包含动态更改的数据的元素都应具有此类的class和inusd属性:

Now... Whatever the structure is, the element containing dynamically changed data should have class and inusd attributes like this:

<li class="price" inusd="123">123</li>

美元中的

价格应存储到一个属性中,以使其独立于将要更改的dom内容.

th price in usd should be stored to an attribute to keep it independant from the dom content that will change.

因此在上面的示例中,我们这样做:

so for the above example we do this:

$('.price').each(function() {
  $(this).text()=$(this).attr('inusd')*USDtoEUR;  //If user selected EUR
}

请谨慎使用switch语句中的breaks和default ... javascript switch可以使案例放弃条件.

Be carefull to use breaks and default in the switch statement... javascript switch lets cases drop their conditions through.

另外,在使用金钱时,请对计算功能进行深入测试,javascript有时也会使upp混乱:D

Also, when working with money, please do the in depth testing of the calculating functions, javascript can mess that upp, too, from time to time :D

这篇关于根据所选选项更改表数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆