【问题标题】:jquery function on radio button checked not working选中的单选按钮上的 jquery 功能不起作用
【发布时间】:2016-01-07 20:47:48
【问题描述】:

我在 html 中的单选按钮:

    <input type='radio' name='sample' id='dynamic' value=dynamic          required='true'> 

单选按钮的我的 jquery 函数:

    $(document).ready(function() {
    $("input:radio").on("click", function() 
    {
        alert('hieeee');
    });
    });

它不能正常工作 我也尝试过以下代码

    $('input:radio[name="sample"]').change(function(){
    if ($(this).is(':checked'))
    {
       alert('hieee');
    }
    });

但是这两个功能都不适合我

【问题讨论】:

  • 您的代码应该可以工作。您是否在控制台中看到任何错误?
  • 您的页面中包含 jQuery 吗?检查浏览器控制台是否有任何错误?
  • its not properly working 是什么意思???如果它是一个动态元素(id='dynamic'?!),你应该委托事件

标签: jquery radio-button


【解决方案1】:

试试这个

 $('input:radio[name="postage"]').change(
function(){
    if ($(this).is(':checked') && $(this).val() == 'Yes') {
        // append goes here
    }
});
Or, the above - again - using a little less superfluous jQuery:

$('input:radio[name="postage"]').change(
function(){
    if (this.checked && this.value == 'Yes') {
        // note that, as per comments, the 'changed'
        // <input> will *always* be checked, as the change
        // event only fires on checking an <input>, not
        // on un-checking it.
        // append goes here
    }
});

【讨论】:

    猜你喜欢
    • 2018-02-14
    • 2015-07-18
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-04
    • 1970-01-01
    • 2013-12-27
    相关资源
    最近更新 更多