【问题标题】:Radio Input gets unchecked after jQuery html operationjQuery html 操作后,Radio Input 未选中
【发布时间】:2014-07-11 23:47:54
【问题描述】:

我遇到了一个非常奇怪的错误。执行以下操作后,我的单选按钮未选中:

    var $page = $('[data-shortcode-page="' + shortcode + '"]', $webrock).html();

    //CHECKED
    console.log($('[data-shortcode-page="' + shortcode + '"] :checked', $webrock).length) 
    $('.webrock-page-content', $addPage).replaceWith($page);
    //UNCHECKED
    console.log($('[data-shortcode-page="' + shortcode + '"] :checked', $webrock).length)

有人知道为什么会这样吗?这是一个 jsFiddle:http://jsfiddle.net/mVB2q/1/

非常感谢!

【问题讨论】:

  • 我想这与.replaceWith($page)有关。
  • 你能加点小提琴吗?
  • 这是一个小提琴:jsfiddle.net/mVB2q/1
  • 如何重现错误?选择选项时应该调用此代码吗?

标签: javascript jquery html radio checked


【解决方案1】:

您正在克隆同名的广播组。您需要更新克隆无线电组的名称。这是一个简单的解决方案,我在“test1”中硬编码新组名,但您可能需要修改它以满足您的需要:

var shortcode = 'object';        

var $page = $('[data-shortcode-page="' + shortcode + '"]');

console.log($('[data-shortcode-page="' + shortcode + '"] :checked').length);

//after cloning the radio buttons, find radio buttons and update the name attribute.
$('.webrock-page-content').html($page.clone().find("input[type='radio']").attr("name", "test1").end().html());
console.log($('[data-shortcode-page="' + shortcode + '"] :checked').length);

Updated fiddle.

【讨论】:

  • 嗨帕特里克,我也考虑过用这种方式修复它。我知道它现在不起作用的原因。谢谢!
猜你喜欢
  • 2021-10-09
  • 1970-01-01
  • 2022-07-01
  • 1970-01-01
  • 2018-03-28
  • 2015-09-28
  • 1970-01-01
  • 2018-05-21
  • 1970-01-01
相关资源
最近更新 更多