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.

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

报告相同问题?

悬赏问题

  • ¥15 WPF动态创建页面内容
  • ¥15 如何对TBSS的结果进行统计学的分析已完成置换检验,如何在最终的TBSS输出结果提取除具体值及如何做进一步相关性分析
  • ¥15 SQL数据库操作问题
  • ¥100 关于lm339比较电路出现的问题
  • ¥15 Matlab安装yalmip和cplex功能安装失败
  • ¥15 加装宝马安卓中控改变开机画面
  • ¥15 STK安装问题问问大家,这种情况应该怎么办
  • ¥15 关于罗技鼠标宏lua文件的问题
  • ¥15 halcon ocr mlp 识别问题
  • ¥15 已知曲线满足正余弦函数,根据其峰值,还原出整条曲线