我使用javascript将数组发布到PHP文件,结果放入div。PHP的结果是一组单选按钮,但是当我尝试使用javascript选择它们时,它不起作用。注意:post函数工作得很好。
Javascript Post(第一个文件)
$.post('newUserTeams.php', { 'teams[]': teams },
function(result) {
$('#tab3').html(result);
});
PHP echo(第二个文件)
echo("<label class='radio'>");
echo("<input type='radio' name='team".$teams[$i]."' id='hi'>");
echo("<i></i>Full Privilege User</label>");
echo("<label class='radio'>");
echo("<input type='radio' name='team".$teams[$i]."'>");
echo("<i></i>Limited Access User</label>");
选择单选按钮(第一个文件)
$("#hi").change(function(){
alert($(this).val());
});
这是因为当元素尚未准备好时,您正在将"change"事件处理程序连接到元素"#hi"。为此,您必须使用"on"方法来绑定事件处理程序。
所以,在你的第一个文件中,编辑你的代码:$(document).on('change', '#hi', function () {
alert($(this).val());
});
因为在你从php中ajax 'HTML'之后,你应该在post回调中添加change事件。
所以你的post函数应该改成:
$.post('newUserTeams.php', { 'teams[]': teams },
function(result) {
$('#tab3').html(result);
$("#hi").on('change', function(){
alert($(this).val());
});
});
我不知道你在哪里调用这个函数,但它可能是由于当页面文档准备执行它没有完成。您需要做的是在创建后添加方法更改:
$( document ).ready(function() {
$.ajax({
url: "objects.php",
}).done(function( data ) {
$('#tab3').html(data);
$('#hi').on('change', function()
{
alert($(this).val());
}
);
});
});