donglv7097 2018-04-25 11:59
浏览 52
已采纳

undefined在警告框jquery ajax中返回

The following code resides in my php file (cl.php):

<?php for( $i = 0; $i < count( $getClFormData->data ); $i++ ) :?>
<tr>
    <input type='hidden' name='itemid[]' value='<?php echo $getClFormData->data[$i]['c_item_id'] ?>'>
    <td><?php echo $getClFormData->data[$i]['item'] ?></td>
    <td><?php echo $getClFormData->data[$i]['description'] ?></td>
    <td><?php echo $getClFormData->data[$i]['norm'] ?></td>
    <td>
        <input type='radio' name='kforce_mont1_<?php echo $i ?>' value='1' class='bobtailradiook'>OK<br/>
        <input type='radio' name='kforce_mont1_<?php echo $i ?>' value='0' class='bobtailradionok' alt='<?php echo $getClFormData->data[$i]['c_item_id'] ?>'>NOK
    </td>
</tr>
<?php endfor;

where <input type='radio' name='kforce_mont1_<?php echo $i ?>' value='0' class='bobtailradionok' alt='<?php echo $getClFormData->data[$i]['c_item_id'] ?>'>NOK is relevant to my question.

Further down cl.php, the following code:

if(isset($_POST['itemid'])){
    <input type='hidden' class='test' value='<?php echo $_POST['itemid']; ?>'>
}

JS Code

$( document ).ready(function() {
    $('.bobtailradionok').each(function() {
        $( this ).click(function() {
            //$("#errortable").show();
            //alert($(this).attr('alt'));
            $.ajax({
                type: 'POST',
                data: {itemid: $(this).attr('alt')},
                url: "cl.php",
                success:function(data){
                    alert($('.test').val());
                    console.log(data);
                }
            });
        });
    });
});

The jquery above returns 'undefined' in the alertbox, while I want it to return the itemid. Why is 'undefined' returned? Since, <input type='hidden' class='test' value='<?php echo $_POST['itemid']; ?>'> is correctly returned as <input type='hidden' class='test' value='1'> or <input type='hidden' class='test' value='2'> depending on the box clicked, when I look in the console.log.

  • 写回答

1条回答 默认 最新

  • download20151010 2018-04-25 12:03
    关注

    Where do you ever add the AJAX response to the page?

    This is showing you what's in the response:

    console.log(data)
    

    But this is looking for a class="test" element on the page:

    $('.test').val()
    

    Did you mean to add the response to the page somewhere first? For example:

    $('#someElement').append(data);
    alert($('.test').val());
    

    Or perhaps specifically look in the response for the value?:

    $('.test', data).val()
    

    or:

    $(data).find('.test').val()
    

    Basically you're just looking for your class="test" element in the wrong place.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图