简体   繁体   English

通过 ajax 加载的元素不能在 jquery 中使用

[英]Elements loaded via ajax can't be used in jquery

I tried to put my data.tables in another file and load that via ajax, sadly the form submission doesn't work anymore.我试图将我的 data.tables 放在另一个文件中并通过 ajax 加载它,遗憾的是表单提交不再有效。

I just get the following error: Uncaught TypeError: this.type is undefined , the idRow Value is still submitted (I can print that to console) so at the moment I don't get why it says that it is undefined.我只是收到以下错误: Uncaught TypeError: this.type is undefined ,idRow 值仍在提交(我可以将其打印到控制台)所以目前我不明白为什么它说它是未定义的。

When all was in a single page the following was working without Problems:当所有内容都在一个页面中时,以下内容可以正常工作:

Form creation and submission after button click:单击按钮后创建和提交表单:

<script>
        function submitForm(rowid) {
            form = document.createElement("form");
            form.action = "save_tables.php"; /
            form.method = "post"; 
            form.style.display = "none";
            $("#"+rowid+" td").children().each(function() { 
                if(this.type.substring(0,6) == "select") { 
                    input = document.createElement("input"); 
                    input.name = this.name;
                    input.type = "hidden";
                    input.value = this.value;
                    form.appendChild(input);
                } else {
                    $(this).clone().appendTo(form);
                }

            });
            document.body.appendChild(form);
            form.submit();
        }
</script>

Example for one entry in the table表中一个条目的示例

<tr id="entry_4" class="row100 body">
<td><input type="text" name="lang"/></td>
<td><input type="text" name="l_code"/></td>
<td><input type="text" name="code"/></td>
<td><button onclick="submitForm('entry_4')">Change</button></td>
</tr>

I replaced the table with the following and then loaded the tables via ajax.我用以下内容替换了表格,然后通过 ajax 加载了表格。

<div align="center" id="selection">
<button onclick="showTables('lang')">Lang</button>
<button onclick="showTables('cat')">Cat</button>
</div>

<div id="target">
</div>
<script>
    function showTables(showtype) {
        $.get(`load_tables.php?show=${showtype}`, function(data) {
            $('#target').html(data);
        })
    }
</script>

Found the problem and a fix, even though I don't know why it happens.找到了问题并进行了修复,尽管我不知道为什么会这样。

When loading the table via ajax, this.type is undefined for some inputs so I changed the if(this.type.substring(0,6) == "select") {通过 ajax 加载表时,某些输入未定义 this.type,因此我更改了if(this.type.substring(0,6) == "select") {

to the following to get it working:到以下让它工作:

if(typeof this.type.= 'undefined' && this.type,substring(0,6) == "select") {

All inputs are submitted (even if there is a select one) so it works for me所有输入都已提交(即使有 select 一个)所以它对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM