【问题标题】:POST Result Select not workingPOST 结果选择不起作用
【发布时间】:2014-07-01 02:50:09
【问题描述】:

我正在使用 javascript 将数组发布到 PHP 文件,并将结果放入 div。 php 的结果是一组单选按钮,但是当我尝试使用 javascript 来选择它们时,它不起作用。 注意: post 功能可以正常工作。

Javascript 帖子(第一个文件)

   $.post('newUserTeams.php', { 'teams[]': teams },
    function(result) {
        $('#tab3').html(result);
    });

PHP 回显(第二个文件)

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>");

Javascript 选择单选按钮(第一个文件)

 $("#hi").change(function(){
     alert($(this).val());
 });

【问题讨论】:

  • $("#hi").on( "change", function() { ..

标签: javascript php jquery html


【解决方案1】:

这是因为当元素尚未准备好时,您将“更改”事件处理程序连接到元素“#hi”。为此,您已使用“on”方法绑定事件处理程序。

因此,在您的第一个文件中,将您的代码编辑为:

$(document).on('change', '#hi', function () {
  alert($(this).val());
});

【讨论】:

    【解决方案2】:

    因为在你从 php 中 ajax 'HTML' 之后,你应该在 post 回调中添加 change 事件。

    所以你的 post 函数应该变成这样:

    $.post('newUserTeams.php', { 'teams[]': teams },
    function(result) {
         $('#tab3').html(result);
         $("#hi").on('change', function(){
             alert($(this).val());
         });
    });
    

    【讨论】:

      【解决方案3】:

      我不知道你在哪里调用这个函数,但这可能是由于页面文档就绪时执行它没有完成。您需要做的是在创建后添加方法更改:

      $( document ).ready(function() {
          $.ajax({
            url: "objects.php",
          }).done(function( data ) {
              $('#tab3').html(data);
              $('#hi').on('change', function()
              {
                  alert($(this).val());
              }
              );
          });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-17
        • 2016-01-23
        • 1970-01-01
        • 2019-04-16
        • 2013-03-20
        • 2014-01-23
        • 2020-01-07
        • 1970-01-01
        相关资源
        最近更新 更多