doumi4974 2015-07-29 03:44
浏览 48
已采纳

jQuery事件没有绑定到php循环创建的元素

I have a form like the one shown below, where after a submit occurs php reloads all the posted values back into the page, but still allows the selection of new rows beneath the reloaded rows.

<script>
$(document).ready(function(){
$(document.body).on('change','.item', function(){
//set cboType options by ajax
//do stuff
});
});
</script>
.....
<table>
<?php
    for($nIndex = 0; $nIndex < count($m_arrItemsList) - 1;$nIndex++)
    {
?>
    <tr>
        <td>
            <select class="item" name="cboItem[]">
<?php
        foreach($m_arrItemsList as $key => $value)
        {
             $strSelect ='';
             if($_POST['cboItem'][$nIndex] == $value['ItemID_PK'])
                 $strSelect = ' selected="selected"';
?>
                <option value="<?=$value['ItemID_PK']?>" <?=$strSelect?>><?=$value['ItemName']?></option>
<?php
        }
?>
            </select>
        </td>
        <td>
            <select class="type" name="cboType[]">
<?php
        foreach($m_arrTypeList as $key => $value)
        {
             $strSelect ='';
             if($_POST['cboType'][$nIndex] == $value['ItemID_PK'])
                 $strSelect = ' selected="selected"';
?>
                <option value="<?=$value['ItemID_PK']?>" <?=$strSelect?>><?=$value['ItemName']?></option>
<?php
        }
    }
?>
            </select>
        </td>
        <td>
            <input type="text" name="txtRate[]" class="rate" value="<?=$_POST['txtRate'][$nIndex]?>">
        </td>
    </tr>
<?php
}
?>
    <tr>
        <td>
            <select class="item" name="cboItem[]">
<?php
    foreach($m_arrItemsList as $key => $value)
    {
?>
                <option value="<?=$value['ItemID_PK']?>" <?=$strSelect?>><?=$value['ItemName']?></option>
<?php
    }
?>
            </select>
        </td>
        <td>
            <select class="type" name="cboType[]">
<?php
    foreach($m_arrTypeList as $key => $value)
    {
?>
                <option value="<?=$value['ItemID_PK']?>" <?=$strSelect?> disabled="disabled"><?=$value['ItemName']?></option>
<?php
    }
?>
            </select>
        </td>
        <td>
            <input type="text" name="txtRate[]" class="rate" value="0">
        </td>
    </tr>
</table>
<button type="submit" name="cmdSave" value="Save">

My issue is that the jquery function after submission does not run for the rows created in the php for loop; however it does work perfectly before submission.

There are also no errors in my javascript console, before or after submission, and the jquery function does run for the not POSTed cboItem after the php loop after submission and the drop downs that are created by said function.

Does anyone have any ideas on how to fix this problem? Any help is appreciated.

  • 写回答

1条回答 默认 最新

  • duanhanzi8328 2015-07-29 03:52
    关注

    If you're creating the html by php, which means the code is being written and loaded and finished when you call $(document).ready(), you shouldn't have to write you're event handler like that.

    Did you try

    $('.item').on('change', function(){});
    

    Also, I'm sure you've done this but I like to put lots of console.logs in my javascript when I don't see any effect. I'd put on just after the .ready(), to see if the javascript is triggering when the page is ready, and another one inside the event handler function, to see if it's the code inside the event handler that's failing to have an effect.

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

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝