问题描述
我有此代码:
<script>
$(document).ready(
function()
{
$('#radans').on("click",function(event)
{
$('#ansform').append("<input type='submit' value='accept answer'>")
});
});
</script>
我有几个id ="radans"的RadioButton和id ="ansform"的表单.当我单击第一个RadioButton处理程序时捕获此事件,但是当我单击第二个,第三个RadioButton处理程序时,处理程序未捕获此事件.我怎么解决这个问题?
And i have several RadioButton with id = "radans" and form with id = "ansform".When i click on first RadioButton handler catches this event,but when i clicked on second,third RadioButton, handler doesn't catch this event. How can i solve this problem?
更新:
<form id = "ansform" action=/question method="post">
<p>Answers:</p>
<input id ="radans" type="radio" name="ansvar" value="0"> yes<br>
<input id ="radans" type="radio" name="ansvar" value="1"> no<br>
谢谢大家.确实我有一个重复ID的严重错误
Thanks for all. Really i have awful mistake with duplicate ID
推荐答案
并且我有几个id =" radans的RadioButton"
"And i have several RadioButton with id = "radans""
这是错误的. ID应该是唯一的.你不能有很多.
This is wrong. Id should be unique. You cant have many.
在您的示例中,$('#radans').on
仅针对第一个ID激活.
In your example $('#radans').on
activates only for the first id.
例如尝试在您的单选按钮上添加相同的类
Try to add same class on your Radiobuttons for example
<input class ="radans" type="radio" name="ansvar" value="0"> yes<br>
<input class ="radans" type="radio" name="ansvar" value="1"> no<br>
,然后使用以下代码:
$(document).ready(
function()
{
$('.radans').on("click",function(event)
{
$('#ansform').append("<input type='submit' value='accept answer'>")
});
});
这篇关于单击单选按钮的处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!