javascript 从循环中的输入字段获取值 onblur

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15080488/
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-26 23:24:38  来源:igfitidea点击:

Get value onblur from input fields in a loop

javascriptloopsonblur

提问by LITguy

Below is how I made an alert showing the "dynamic" id using onblur. I have a while loop with input fields so the id is pulling from a unique id in a database.

下面是我如何使用 onblur 显示“动态”ID 的警报。我有一个带有输入字段的 while 循环,因此 id 是从数据库中的唯一 id 中提取的。

How would I get the value too? Normally I would just set a variable like usual but it's in a loop so I'm not understanding how.

我将如何获得价值?通常我会像往常一样设置一个变量,但它处于循环中,所以我不明白如何。

<script type="text/javascript">
    function doUpdateEntries(clicked_id) {
        alert(clicked_id);
        // how do I get the value AND id?
    }
</script>

while ($row = mysql_fetch_assoc($result)) {
    <input onblur="doUpdateEntries(this.id);" id=".$row['entry_id']." name=".$row['entry_id']." type="text" value=".$row['reviews'].">
}

回答by JonWarnerNet

You could simply pass through the value to the function doUpdateEntriesas another parameter.

您可以简单地将值doUpdateEntries作为另一个参数传递给函数。

<script type="text/javascript">
    function doUpdateEntries(id, value) {
        alert(id);
        alert(value);
    }
</script>

while ($row = mysql_fetch_assoc($result)) {
    <input onblur="doUpdateEntries(this.id, this.value);" id=".$row['entry_id']." name=".$row['entry_id']." type="text" value=".$row['reviews'].">
}

回答by james emanon

As Ankit mentions… just pass in this, à la

正如 Ankit 提到的……只是传入this,à la

<input onblur="doUpdateEntries(this);" id=".$row['entry_id']." name=".$row['entry_id']." type="text" value=".$row['reviews'].">

Below is how I made an alert showing the "dynamic" id using onblur. I have a whileloop with input fields so the idis pulling from a unique id in a database.

下面是我如何使用onblur. 我有一个while带有输入字段的循环,因此它id是从数据库中的唯一 ID 中提取的。

How would I get the value too? Normally I would just set a variable like usual but it's in a loop so I'm not understanding how.

我将如何获得价值?通常我会像往常一样设置一个变量,但它处于循环中,所以我不明白如何。

function doUpdateEntries(_this) {
    var _id = _this.id;
    var _name = _this.value;
}