【问题标题】:Send data inside an html type='button'在 html type='button' 中发送数据
【发布时间】:2014-03-05 23:08:00
【问题描述】:

我有一个带有注意消息的 div。客户端可以在最右上角放置一个子项(输入类型='按钮')来关闭此 div。

我的目标是关闭消息 30 分钟,然后它会重新出现。为此,我需要将当前时间发送到 ajax,然后将其发布到 session['field']。

我真的不确定从按钮内部发送数据的正确方法是什么。

我发现了以下作品.. 但我只是在编造一些不好的随机属性?

PHP

<input type="button" class="close" crazyfieldnametime="<?php echo time(); ?>">

jQuery

$(".close").click(function(){
    alert($(this).attr("crazyfieldnametime"));
});

是否有这样做的最佳实践?

【问题讨论】:

    标签: javascript php jquery html ajax


    【解决方案1】:

    使用 HTML5 data-* 属性,数据属性名称必须至少有一个字符长,并且必须以'data-'为前缀。它不应包含任何大写字母。

    PHP

    <input type="button" class="close" data-crazyfieldnametime="<?php echo time(); ?>">
    

    jQuery

    $(".close").click(function(){
        alert($(this).data("crazyfieldname"));
    });
    

    【讨论】:

    【解决方案2】:

    您可以使用 html 允许的自定义数据属性

    例如

    <input type="button" class="close" data-time="<?php echo time(); ?>">
    

    然后

    $(".close").click(function(){
        alert($(this).data("time"));
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-26
      • 1970-01-01
      • 1970-01-01
      • 2014-10-15
      • 1970-01-01
      • 2015-11-14
      • 1970-01-01
      • 2017-03-25
      相关资源
      最近更新 更多