【问题标题】:Input type button not working within table tds输入类型按钮在表 tds 中不起作用
【发布时间】:2017-08-23 20:13:42
【问题描述】:

我确实提到了这个fiddle 和这个post,但这并不能解决我的问题。因为我使用了 CSP,所以我很清楚,onclick 属性不起作用,因此我使用 jQuery .on('click') 来完成这项工作,但仍然没有满意的结果。 这是我的代码结构(将仅包括与此问题相关的代码):-

html

<td><input value="Resend" class="btn btn-primary btn-xs" id="resend_sms" name="resend_sms" type="button"></td>

脚本

$(function(){
            $('#resend_sms').on('click', function(e){
                console.log('clicked');
            });
        });

以上是相关的代码。请注意,这部分 html 是由服务器端脚本生成的。再次,我重新测试了这个家伙,但点击后仍然没有结果。

【问题讨论】:

  • 使用这个 $("[id$=resend_sms]")
  • @SarikaKoli 感谢您的建议。但是,我更喜欢this 解决方案,因为它适用于我的情况。不胜感激。

标签: jquery html button html-table


【解决方案1】:

它工作正常。我刚刚将 jquery 文件添加到您的代码中

$(function() {
  $('#resend_sms').on('click', function(e) {
    console.log('clicked');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td><input value="Resend" class="btn btn-primary btn-xs" id="resend_sms" name="resend_sms" type="button"></td>

【讨论】:

  • 我正在使用上面已经在头文件中调用的引导模板和脚本调用。无论如何,感谢您的帮助。
【解决方案2】:

如果您的 html 部分是动态生成的,或者是从服务器端脚本生成的:

然后像这样使用它:

       $(function(){
            $(document).on('click','#resend_sms', function(e){
                console.log('clicked');
            });
        });

这会起作用。

【讨论】:

  • 太棒了!我使用了这个答案并且它正在工作。你介意我想知道这背后的原因吗?只是为了提高我对这类东西的逻辑思维。谢谢。 =D
  • @WafieAli 因为所有的 jquery 事件都与文档相关联,所以我们必须使用文档方法来动态添加 html。更多详情可以访问stackoverflow.com/questions/14879168/…
  • 感谢小伙伴的分享!我会保留这个以供将来参考。
猜你喜欢
  • 2014-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多