duangai2831 2011-01-11 07:39
浏览 216
已采纳

如何在jquery中获取textbox的当前值

I'm trying to compute a value from 2 given values quantity and price. I used the keyup event in jquery to compute the total from the current value of the qty and price. How will I be able to reference the current textbox which initiates the keypress event. And put the corresponding total to the assigned box.
Do you know of any jquery plugins that will make it easier for me to achieve the result.

<script type="text/javascript">
$(function(){

$('input[name^=qty]').keyup(function(){

    var curprice=$('input[name^=price]').val();
    var curqty=$('input[name^=qty]').val();
    var curtotal=curprice * curqty;

    $('input[name^=comp]').val(curtotal);
});




});

</script>

</head>
<body>




<?php
for($a=0;$a<=5;$a++){
    ?>


    Price:<input type="text" name="<?php echo 'price'.$a; ?>" value="<?php echo $a; ?>"/>
    Quantity:<input type="text" name="<?php echo 'qty'.$a; ?>" value="<?php echo $a; ?>"/>
    Computed:<input type="text" id="<?php echo 'a'.$a; ?>" name="<?php echo 'comp'.$a ?>" value="" />


    <?php
echo "<br/>";
}

?>




</body>
</html>

Heres a screenshot. alt text

If you need more details, feel free to ask, thanks.

  • 写回答

2条回答 默认 最新

  • dqx24298 2011-01-11 07:46
    关注
    <script type="text/javascript">
    $(function(){
    
    $('input[name^=qty]').keyup(function(){
    
        var parentDiv = $(this).closest('div');
        var curprice=parentDiv.find('input[name^=price]').val();
        var curqty= this.value;
        var curtotal=curprice * curqty;
    
        parentDiv.find('input[name^=comp]').val(curtotal);
    });
    
    
    
    
    });
    
    </script>
    
    </head>
    <body>
    
    
    
    
    <?php
    for($a=0;$a<=5;$a++){
        ?>
    
        <div>
    
        Price:<input type="text" name="<?php echo 'price'.$a; ?>" value="<?php echo $a; ?>"/>
        Quantity:<input type="text" name="<?php echo 'qty'.$a; ?>" value="<?php echo $a; ?>"/>
        Computed:<input type="text" id="<?php echo 'a'.$a; ?>" name="<?php echo 'comp'.$a ?>" value="" />
    
        </div>
        <?php
    echo "<br/>";
    }
    
    ?>
    
    
    
    
    </body>
    </html>
    

    notice all the changes against your current code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探