Jquery 获取Ajax创建的单选按钮的值
我有一个Ajax调用,它生成一组单选按钮。现在我需要选择一个单选按钮并执行后续操作。单选按钮显示正确,但不返回单选按钮选项 如何访问Ajax响应生成的单选按钮 主要代码:Jquery 获取Ajax创建的单选按钮的值,jquery,ajax,Jquery,Ajax,我有一个Ajax调用,它生成一组单选按钮。现在我需要选择一个单选按钮并执行后续操作。单选按钮显示正确,但不返回单选按钮选项 如何访问Ajax响应生成的单选按钮 主要代码: <!DOCTYPE html> <html> <head> <title>AJAX TEST</title> <style> div{ margin-left:100px; }
<!DOCTYPE html>
<html>
<head>
<title>AJAX TEST</title>
<style>
div{
margin-left:100px;
}
#response{
padding-top:40px;
}
</style>
<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
</head>
<body>
<div id="header">
<h1>Click This Button</h1>
<button name="button" id="button" type="button" class="favorite styled"
type="button">
Click Here
</button>
</div>
<div id="response" name="response">
</div>
<div id="radioResponse">
</div>
</body>
</html>
<?php
// jquery ajax to show choices
?>
<script>
$("#button").click(function() {
console.log('button clicked');
$.get("ajax1.php", function(data){
$("#response").append(data);
});
});
$('#radio input[type="radio"]').change(function(){
var demovalue = $(this).val();
console.log(demovalue);
alert("Your result is "+demovalue);
});
</script>
AJAX测试
div{
左边距:100px;
}
#回应{
填充顶部:40px;
}
点击这个按钮
点击这里
$(“#按钮”)。单击(函数(){
log(“单击按钮”);
$.get(“ajax1.php”,函数(数据){
$(“#响应”)。追加(数据);
});
});
$('#无线电输入[type=“radio”]')。更改(函数(){
var demovalue=$(this.val();
console.log(demovalue);
警报(“您的结果是”+demovalue);
});
ajax1.php的代码是
<?php
$choices = <<<"CHOICES"
<div id="radio">
<p><input type="radio" id="a" name="choices"><label for="a"> Choice A</label></p>
<p><input type="radio" id="b" name="choices"><label for="b"> Choice B</label></p>
<p><input type="radio" id="c" name="choices"><label for="c"> Choice C</label></p>
</div>
CHOICES;
echo $choices;
?>
通过更改以下内容使用事件委派:
$('#radio input[type="radio"]').change(function(){
致:
请注意,要使单选按钮显示值.val()
,输入
元素必须具有具有相应值的值
属性。例如:
<input type="radio" id="b" value="B" name="choices">
选择A
选择B
选择C
这让我走到了一半。将显示警报,但每个警报都会显示“您的结果已打开”,而不是“您的结果为A”或“B”或“C”。@DougHemingway请添加一个值属性;请参阅上面的更新。
<input type="radio" id="b" value="B" name="choices">