douzhongqiu5032 2018-01-14 18:24
浏览 246
已采纳

从id =和value =的<input>动态值中获取并使它们变量以在javascript中使用它们

I have clockpicker (https://weareoutman.github.io/clockpicker/) as a tool to input time on my php page, which contains a lot of similar but unique fields.

It works like this:

<?php while // ...extracting data from mysql

// and we have got a lot of "cards" (Their quantity is not constant and there can be from 30 to 100 cards), each one of them has unique card_numuber and card_time
echo <div id='card'> ;
echo <div id='$row['card_numuber']'> $row['card_numuber']</div> ;
echo <div class='input-group clockpicker-with-callbacks'><input id='$row['card_numuber']' type='text' class='form-control' value=".$row['card_time']."></div>;
echo </div> ;
?>

And this is my script I use to get data to variables:

<script type="text/javascript">
$('.clockpicker-with-callbacks').clockpicker({
        donetext: 'Done',
        afterDone: function() {
        var card_time = ??? // here is I have no idea how to get the~ <input value=".$row['card_time']."> 
        var card_numuber = ??? // and here I've got no clue how to mage var from dynamic id='$row['card_numuber']' value

        console.log(card_numuber, card_time);
        $.post( "2_settime.php", { 
            card_numuber: card_numuber, card_time: card_time
        })

        }
    })
</script>

I need somehow to get the data... from html and pass it to javascript... So:

card_time IS value=".$row['card_time']."

card_numuber IS id='$row['card_numuber']'

How can I get this such a horrible task done? Please, help.

  • 写回答

1条回答 默认 最新

  • dongxi1965 2018-01-14 18:28
    关注

    Sadly, looking at the source, that plugin doesn't pass the afterDone callback anything useful. So in order to know what input is related to it, we'll have to hook things up in a more verbose way:

    // Normally, you'd avoid using `each` like this, but sadly this plugin calls
    // its callbacks with NO context information
    $('.clockpicker-with-callbacks').each(function() {
        var input = $(this).find("input")[0];
        $(this).clockpicker({
            donetext: 'Done',
            afterDone: function() {
                var card_time = input.value; // The current value of the input
                var card_numuber = input.id; // The input's ID
    
                console.log(card_numuber, card_time);
                $.post( "2_settime.php", { 
                    card_numuber: card_numuber, card_time: card_time
                });
            }
        });
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装
  • ¥40 复杂的限制性的商函数处理