简体   繁体   English

jQuery Datatables-无法从隐藏页面获取输入值

[英]jQuery Datatables - Can't get input value from hidden pages

I have this table in my HTML: 我的HTML中有此表:

<table>
    <thead>
        <tr>
            <th>Id</th>
            <th>Actual weight</th>
            <th>New weight</th>
        </tr>      
    </thead>

    <tbody>
        <tr>
            <td>1</td>
            <td>10</td>
            <td><input data-id="{{ object.id }}" type="number" name="input_new_weight" /></td>
        </tr>

        <tr>
            <td>1</td>
            <td>20</td>
            <td><input data-id="{{ object.id }}" type="number" name="input_new_weight" /></td>
        </tr>

        <!-- multiply by 10 the number of trs -->
    </tbody>
</table>

And I have this code in my javascript to get the input values: 而且我的JavaScript中有以下代码来获取输入值:

var new_weights = []
$("input[name='input_new_weight']").each(function(index, element){

    if( $(this).val() != "" ){
        var object_new_weight ={
            id: $(this).data('id'),
            new_weight: $(this).val()
        }
        new_weights.push(object_new_weight);
    }

});
console.log(new_weights);

I'm using jQuery DataTables plugin to generate the tables and have the possibili ty to filter, paginate, ordenate and etc. 我正在使用jQuery DataTables插件来生成表,并具有过滤,分页,编排等功能。

In some tables I have more than 10 entries, so the paginations works here. 在某些表格中,我有10多个条目,因此分页在这里起作用。 In the example above, it will be 2 pages: 1 and 2. 在上面的示例中,它将是2页:1和2。

When my javascript code is executed, it does only gets the inputs values from the visible table page. 执行我的JavaScript代码时,它只会从可见表格页面获取输入值。 The inputs from the hidden pages are not get! 隐藏页面的输入无法获取!

Let's suppose that in page 1 I put the new weight values as 35, 75 and 80 and in the page 2 I put 40, 54, 97. When my javascript code runs, it does just get the values from the visible page. 假设在第1页中,我将新的权重值分别设置为35、75和80,在第2页中,我将新的权重值分别设置为40、54、97。当我的javascript代码运行时,它只是从可见页中获取值。

Please, can someone tell me why this is happening? 拜托,有人可以告诉我为什么会这样吗?

It's really straightforward you know datatable generates table on the fly so when you are on page 1, inputs corresponding to page 2 (40, 54, 97) aren't there at all on the page. 您知道数据表是动态生成表的,这非常简单,因此,当您在第1页上时,页面上根本没有对应于第2页的输入(40、54、97)。

So I am guessing you have put your this code out in the global 所以我想你已经把这个代码发布到了全球

$("input[name='input_new_weight']").each(function(index, element){
    //stuff
});

This runs only one time; 这只运行一次; on initial loading of your page when inputs from only page 1 are there, what you rather need is to be able to run your code every-time datatable re-renders.There's a hook that you may use page.dt 当只有页面1的输入在那里时,在页面的初始加载时,您宁愿需要的是能够每次重新运行datatable时重新运行代码。您可以使用page.dt

Put this after the code where you initialize datatable 将其放在初始化数据表的代码之后

$('#yourtable').on('page.dt', function(){

    $("input[name='input_new_weight']").each(function(index, element){
        //stuff
    });

});

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

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