[英]JQuery with radio buttons not working
I am creating a 'my account page as part of a larger project. 我正在创建“我的帐户”页面,这是一个较大项目的一部分。 And I am using jquery to get data about which radio button the user has selected. 我正在使用jquery获取有关用户选择了哪个单选按钮的数据。 I cannot figure out why it isnt working, i believe the problem is with the jquery used to dictate when a radio button is clicked. 我不知道为什么它不起作用,我相信问题在于用来指示单击单选按钮时的jQuery。
JQUERY: JQUERY:
$("input[name=currentTree]").on('change',function(){
var currentTreeId = $(this).val();
alert ('data:' + currentTreeId);
$.post('selectTree.php',{currentTreeId:currentTreeId} ,function(data,status){
alert(data + ' is the current tree');
});
});
$("input[type=radio][name=currentTree]").change(function(){
var currentTreeId = $(this).val();
alert ('data:' + currentTreeId);
$.post('selectTree.php',{currentTreeId:currentTreeId} ,function(data,status){
alert(data + ' is the current tree');
});
});
Try this: 尝试这个:
$('input[type=radio][name=currentTree]').change(function() { if ($(this).val() == 'typeA') { alert("Got Type A Radio"); } else if ($(this).val() == 'typeB') { alert("Got Type B Radio"); } });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <label for="typea">Type A</label><input id="typea" type="radio" name="currentTree" value="typeA"> <label for="typeb">Type B</label> <input id="typeb" type="radio" name="currentTree" value="typeB">
$("input[name=currentTree]").on('change', function(){
...
});
Example: https://jsfiddle.net/b80sa4kx/ 示例: https : //jsfiddle.net/b80sa4kx/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.