javascript - 当 jquery 类等于某个值时,如何执行数学运算?

标签 javascript jquery ajax

我正在尝试根据随 jQuery 更改的类名称来操纵数量。我的金额来自这里:

<script>
var inboundAmount = <?php echo json_encode($inbound); ?>;
</script>

假设数量为 10。

我输出了一些 php 和脚本,如下所示:

echo "<a href=\"#inbound\" class=\"$inbound_status first$id\" onclick=\"comtype($id,'inbound', $vendor)\">$<script>document.write(inboundAmount);</script></a>";

当用户点击它时,类会发生变化。示例类如下:

half first3547
pending first3547
paid first3547

我一直在尝试做的是,如果类包含单词“half”,那么它会做一些数学运算:

document.write(inboundAmount/2);

生成数字 5。 我试过这个和一些没有运气的变体:

if ($("#mycontent").find("a.half").length > 0) {document.write(inboundAmount/2);}

'mycontent' 是 href 所在表的 ID。

我也试过以下方法:

if ( $( this ).hasClass( 'half' ) ) { {document.write(inboundAmount/2);}

还是不行。

一个完整的例子是这样的:

<td class='grav-results-td'>
<?php if (!empty($check_inbound)):echo "<a href=\"#inbound\" 
class=\"$inbound_status first$id\" onclick=\"comtype($id,'inbound', 
$vendor)\">$<script>if ( $( this ).hasClass( 'half' ) ) 
{document.write(inboundAmount/2);}</script></a>"; endif; ?></td>

而且我还尝试了在我的 ajax 脚本的成功区域中进行操作。

回答下面的一些问题:

$inbound_status first$id 是一个 PHP 变量。

document.write(inboundAmount/2) 的输出;工作正常并显示一半的 document.write(inboundAmount);

最佳答案

this <script> 内的范围标签不会指向您期望的元素。您可以检查 php 中的条件,而不是检查 javascript。本身。否则,您可以引用下面的代码在 javascript 上执行此操作。您可以使用 php 变量来检查它是否包含字符串 half

echo "<td class='grav-results-td'>";
if (!empty($check_inbound)):
    echo "<a href=\"#inbound\" 
          class=\"$inbound_status first$id\" 
          onclick=\"comtype($id,'inbound',$vendor)\">
          $<script>if ('$inbound_status' == 'half' ) 
                    {document.write(inboundAmount/2);}
          </script>
          </a>"; 
endif; 
echo "</td>";

如果inboundAmount是一个 php 变量,您无需使用 javascript 即可轻松完成

echo "<td class='grav-results-td'>";
if (!empty($check_inbound)):
    echo "<a href=\"#inbound\" 
          class=\"$inbound_status first$id\" 
          onclick=\"comtype($id,'inbound',$vendor)\">$";
    if ($inbound_status == 'half'): 
        echo $inboundAmount/2;
    endif;
    echo "</a>"; 
endif; 
echo "</td>"

关于javascript - 当 jquery 类等于某个值时,如何执行数学运算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50520992/

相关文章:

javascript - Node.js游戏开发,客户端-服务器数据交换逻辑

JavaScript 代码说明

javascript - 使用 javascript 正则表达式将 url 转换为 html <a> 标记和图像 url 到 <img> 标记

javascript - 如何在javascript中清空表格?

Javascript forEach - 在迭代之前等待 setTimeout 完成

javascript - AJAX 或 Socket.IO 在我的情况下更有意义?

ajax - Knockout JS - 使用 AJAX 调用更新 viewModel

javascript - SilverStripe 通过 Ajax 提交 HTML 表单

javascript - 如何使用偏移路径 CSS 属性从其原始位置偏移 SVG 路径

javascript - 如何在javascript/jquery中实现这个键盘 Hook 功能?