检测 html(表单)表中的行数据变化(JavaScript / Jquery)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15661271/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 01:35:07  来源:igfitidea点击:

Detect row data change in html (form) table (JavaScript / Jquery)

javascriptjquery

提问by Frank Zhu

Hi I am a reader and getting a lot of help here. This is my first post. I know this could be a similar question, but I still could not find answer for it. Can someone please help me to write a JavaScript / jquery code: if a user modifies any of these fields, then the dbFlag value will set to "U" (init. as “N”). I am struggling to produce the correct code. Note: the data rows are populated from DB, so it could be 1 row, or N rows. Highly appreciate your time and efforts.

嗨,我是一名读者,在这里得到了很多帮助。这是我的第一篇文章。我知道这可能是一个类似的问题,但我仍然找不到答案。有人可以帮我写一段 JavaScript/jquery 代码:如果用户修改了这些字段中的任何一个,那么 dbFlag 值将设置为“U”(初始化为“N”)。我正在努力生成正确的代码。注意:数据行是从 DB 填充的,所以它可以是 1 行,也可以是 N 行。非常感谢您的时间和努力。

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <form name="form1" id="form1" action="saveData" method="POST">
            <table id="dataTable" border="1">
                <tbody>
                    <tr>
                        <th>Badge #</th>
                        <th>turn #</th>
                        <th>Comment</th>
                    </tr>   
                    <tr>
                        <td><input type="text" size="7" name="badge" id="badge" value="000000" /></td>
                        <td>
                            <select name="descMpoint" id="descMpoint2">
                                <option value="1">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>
                            </select>                    
                        </td>
                        <td><textarea name="comment" cols="50" rows="2">txt area</textarea></td>             
                        <td><input type="hidden" name="dbFlag" id="dbFlag" value="N" />     
                    </tr>
                </tbody>
            </table>
            <br>
            <span class="tab"><input type="submit" name="updateData" value="Submit" id="updateData"/></span>
            <br><hr>
        </form>
    </body>
</html>

回答by Syon

Using the changeevent:

使用change事件:

$('#form1').on('change', 'input, select, textarea', function(){
    $(this).closest('tr').find('input[name="dbFlag"]').val('U');
});

jsFiddle& .change()

jsFiddle& .change()

回答by Esaevian

In jQuery, use the changeevent function. Note that the function fires when the field in question is unfocused/blurred.

在 jQuery 中,使用更改事件函数。请注意,该函数在相关字段未聚焦/模糊时触发。

Example:

例子:

$("#badge").change(function(){
    var currentElement = $(this); //reference to the element that was changed
    //etc. etc.
});

JSFiddle: http://jsfiddle.net/kfdNs/

JSFiddle:http: //jsfiddle.net/kfdNs/