jQuery Access 动态创建的单选按钮

发布于 2024-09-25 13:40:23 字数 855 浏览 0 评论 0原文

我的页面上有 3 个单选按钮。它们是通过调用一个 jQuery 函数来加载的,该函数返回 html ex:

<script type="text/javascript"><!-- 
 onSelectChange(4);
  -->

调用此函数:

function onSelectChange(parm){

        $.post("func.php", {function_name : "song"},
            function(data){
                $("#list").html(data.song_html);
                }, "json");
} 

返回的 html 看起来像这样:

<INPUT TYPE="radio" NAME="songs" VALUE="A">A
<INPUT TYPE="radio" NAME="songs" VALUE="B">B
<INPUT TYPE="radio" NAME="songs" VALUE="C">C
<INPUT TYPE="radio" NAME="songs" VALUE="D">D

现在我想知道何时单击这些单选按钮之一,所以我有以下函数:

 $("input[name='songs']").change( function() {
 alert($(this).val());

但它永远不会触发- 我认为这与动态添加控件有关,因为当我在静态 html 页面上尝试上述操作时,它会按预期触发。有什么想法吗?

I have 3 radio buttons on my page. They are loaded by calling a jQuery function which returns the html ex:

<script type="text/javascript"><!-- 
 onSelectChange(4);
  -->

which call this function:

function onSelectChange(parm){

        $.post("func.php", {function_name : "song"},
            function(data){
                $("#list").html(data.song_html);
                }, "json");
} 

html returned looks like this:

<INPUT TYPE="radio" NAME="songs" VALUE="A">A
<INPUT TYPE="radio" NAME="songs" VALUE="B">B
<INPUT TYPE="radio" NAME="songs" VALUE="C">C
<INPUT TYPE="radio" NAME="songs" VALUE="D">D

Now I want to know when 1 one of these radio buttons is clicked so I have the following function:

 $("input[name='songs']").change( function() {
 alert($(this).val());

It never fires though - I think it has something to do with dynamically adding the controls because when I try the above on a static html page it fires as expected. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞陪衬 2024-10-02 13:40:23

动态添加控件时使用 .live() ,如下所示

$("input[name='songs']").live("change", function() {
  alert($(this).val());
});

:不同的是,它不是查找元素并将 change 处理程序绑定到它们,而是在 document 上绑定一个处理程序,该处理程序侦听 change 事件冒泡...无论何时添加元素,都会以相同的方式发生。

Use .live() when adding controls dynamically, like this:

$("input[name='songs']").live("change", function() {
  alert($(this).val());
});

This works differently, rather than finding elements and binding a change handler to them, it binds a handler on document which listens for the change event to bubble up...and this happens the same way, no matter when the element was added.

九公里浅绿 2024-10-02 13:40:23
$("input[name=songs]").change( function() {
 alert($(this).val());
}

name= 后面没有撇号

$("input[name=songs]").change( function() {
 alert($(this).val());
}

no apostrophe after name=

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文