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 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料