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

时间:2018-05-25 03:16:06

标签: 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

我一直试图做的是,如果课程中包含“半”字样,那么它会做一些数学运算:

document.write(inboundAmount/2);

产生数字5。 我试过这个以及一些没有运气的变化:

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

&#39;为myContent&#39;是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第一个$ id是一个PHP变量。

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

1 个答案:

答案 0 :(得分:1)

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>"