jQuery Access 动态创建的单选按钮
我的页面上有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
动态添加控件时使用
.live()
,如下所示:不同的是,它不是查找元素并将
change
处理程序绑定到它们,而是在document
上绑定一个处理程序,该处理程序侦听change
事件冒泡...无论何时添加元素,都会以相同的方式发生。Use
.live()
when adding controls dynamically, like this:This works differently, rather than finding elements and binding a
change
handler to them, it binds a handler ondocument
which listens for thechange
event to bubble up...and this happens the same way, no matter when the element was added.name= 后面没有撇号
no apostrophe after name=