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

从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.

图片转代码服务由CSDN问答提供 功能建议

我有clockpicker( https://weareoutman.github.io/clockpicker/ )作为在我的php页面上输入时间的工具,其中包含许多类似但独特的字段。 \ n

它的工作方式如下:

 &lt;?php while // ...从mysql中提取数据
 
 //我们有很多 “卡片”(它们的数量不是恒定的,可以有30到100张卡片),每一张卡片都有唯一的card_numuber和card_time 
echo&lt; div id ='card'&gt;  ; 
echo&lt; div id ='$ row ['card_numuber']'&gt;  $行[ 'card_numuber']&LT; / DIV&GT;  ; 
echo&lt; div class ='input-group clockpicker-with-callbacks'&gt;&lt; input id ='$ row ['card_numuber']'type ='text'class ='form-control'value =“。  $ row ['card_time']。“&gt;&lt; / div&gt ;; 
echo&lt; / div&gt;  ; 
?&gt; 
   
 
 

这是我用来获取变量数据的脚本:

   &lt; script type =“text / javascript”&gt; 
 $('。clockpicker-with-callbacks')。clockpicker({
 donetext:'Done',
 afterDone:function(){
 var card_time =  ??? //这里是我不知道如何得到〜&lt; input value =“。$ row ['card_time']。”&gt; 
 var card_numuber = ??? //这里我有 没有线索如何从动态id ='$ row ['card_numuber']'值
 
 console \ log(card_numuber,card_time); 
 $ .post(“2_settime.php”,{
 card_numuber:  card_numuber,card_time:card_time 
})
 
} 
})
&lt; / script&gt; 
   
 
 

我需要以某种方式获取数据.. 。从html中传递给javascript ... So:

card_time IS value =“。$ row ['card_time']。”

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

我怎么能完成这么糟糕的任务呢? 请帮忙。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

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
                });
            }
        });
    });
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题