简体   繁体   English

jQuery,脚本无法识别新添加的元素

[英]jQuery, script doesn't recognize newly appended elements

I have a short script that styles form radio buttons. 我有一个简短的脚本,用于设置单选按钮的样式。 Check working example http://jsfiddle.net/YJRsk/ . 检查工作示例http://jsfiddle.net/YJRsk/

This works great when radio buttons already exist in DOM. 当DOM中已经存在单选按钮时,这非常有用。 So when page load, they get styled properly. 因此,在页面加载时,它们的样式正确。

The problem is when i add radio buttons dynamically by appending them, the script doesn't recognize them since it already ran on page load. 问题是当我通过添加单选按钮动态添加单选按钮时,脚本无法识别它们,因为它已在页面加载时运行。 So for that reason i get an error Uncaught TypeError: Cannot read property 'style' of null. 因此,由于这个原因,我得到一个错误Uncaught TypeError:无法读取null的属性'style'。 How can i work around this so that newly appended radios gets automatically styled on the spot without having to save and refresh the page. 我该如何解决这个问题,以便新添加的收音机可以在现场自动设置样式,而不必保存和刷新页面。 I tried to running the script after each radio append, but that didn't work. 我尝试在每次添加广播后运行脚本,但这没有用。

var item = '<li><input type="radio" value="'+uniqid()+'" class="styled" name="test"/><label class="radioButton">Radio button</label></li>';
$('button').live('click', function() {
    $(item).appendTo('#radio');
})

Check the jsfiddle example and click the "add radio" button to test. 检查jsfiddle示例,然后单击“添加单选”按钮进行测试。

Use a CSS rule and add a class to all radio buttons. 使用CSS规则,并将类添加到所有单选按钮。 If you are using advanced jQuery selectors for styling, I'd recommend simply applying that styling function on creation of each new element. 如果您使用高级jQuery选择器进行样式设置,则建议您在创建每个新元素时简单地应用该样式设置功能。

Edit: The basic problem here is that your .js files are loaded on page load, so if you use something like: 编辑:这里的基本问题是您的.js文件是在页面加载时加载的,因此,如果您使用类似以下内容的文件:

$(".mybutton").click(function() {
    alert("test");
});

This event will only be 'tacked onto' .mybutton elements that exist at page load. 此事件将仅“添加到”页面加载时存在的.mybutton元素上。 If you create/add elements dynamically, you will have to assign the above event handler to them again. 如果动态创建/添加元素,则必须再次将上述事件处理程序分配给它们。

This also has the effect that your uniqid() function will be creating the same id for all radio buttons added currently. 这还会产生影响,您的uniqid()函数将为当前添加的所有单选按钮创建相同的ID。 Do it like this instead: 改为这样做:

$('button').live('click', function() {
    var item = '<li><input type="radio" value="'+uniqid()+'" class="styled" name="test"/><label class="radioButton">Radio button</label></li>';
    $(item).appendTo('#radio');
})

Edit 2: 编辑2:

I don't know what jQuery styling you are using for your radio button, but the basic idea is to use the styling function on your newly created button each time. 我不知道您的单选按钮使用什么jQuery样式,但是基本思想是每次都在新创建的按钮上使用样式功能。 See this fiddle: http://jsfiddle.net/YJRsk/11/ 看到这个小提琴: http : //jsfiddle.net/YJRsk/11/

If I am still missing something, please elaborate and show us the jQuery you use for radio button styling. 如果我仍然缺少某些东西,请详细说明并向我们展示您用于单选按钮样式的jQuery。

If you know the maximum number of radio buttons, this is what I would do: 如果您知道单选按钮的最大数量,这就是我要做的:

  1. Add maximum number of radio buttons to the form (show just those which you wan't to be shown at the beginning) 在表单中添加最大数量的单选按钮(仅显示您不想在一开始显示的按钮)

  2. Leave a script to style them 留下脚本样式

  3. On button click, just show already styled radio buttons one by one 单击按钮时,仅逐一显示已样式化的单选按钮

I hope I was clear enough. 我希望我足够清楚。

You forgot to add a piece of your HTML code in the Javascript. 您忘了在Javascript中添加一段HTML代码。

var item = '<li><span class="radio" style="background-position: 0pt 0pt;"></span><input type="radio" value="'+uniqid()+'" class="styled" name="test"/><label class="radioButton">Radio button</label></li>';

$('button').live('click', function() {
    $(item).appendTo('#radio');
})

http://jsfiddle.net/YJRsk/3/ http://jsfiddle.net/YJRsk/3/

That said, it's probably a smarter idea to style your radio buttons using CSS 也就是说,使用CSS设置单选按钮的样式可能是一个更明智的主意

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM