简体   繁体   English

循环内的javascript函数不起作用

[英]javascript function inside loop not working

I am displaying online users. 我正在显示在线用户。 When I click one of the user, corresponding user should be displayed in the text box below. 当我单击一个用户时,相应的用户应显示在下面的文本框中。 I am using javascript for this, but it is taking only the first user. 我为此使用javascript,但是它仅使用第一个用户。 When I click the second user, first user is displayed in the below text box. 当我单击第二个用户时,第一个用户将显示在下面的文本框中。 Why is it taking only the first array? 为什么只采用第一个数组?

<?php
    foreach($query as $row)
    {
    ?>
        <input type="text" name="user" id="user" value="<?php echo $row->users;?> onclick="select_online()">
<?php
    }
    ?>
    <script>

    function select_online()
    {
        var user=document.getElementById("user").value;
        document.getElementById("usersonline").value=user;
    }
    </script>


Name:<input type="text" name="usersonline" id="usersonline">

First of all, using the same id for many elements is a mistake. 首先,对许多元素使用相同的id是一个错误。 You should make diverse id attribute for generated inputs. 您应该为生成的输入设置不同的id属性。

Second, you should use this to get the value of the current element: 其次,您应该使用this来获取当前元素的值:

function select_online()
{
    var user=this.value;
    document.getElementById("usersonline").value=user;
}

document.getElementById("user") statement will always select the element which id attribute is equal to "user" and it will always be the same. document.getElementById("user")语句将始终选择id属性等于“ user”的元素,并且该元素将始终相同。 Most probably, that is not what you want. 很有可能,这不是您想要的。 If I understood you correctly, you want to get value of the clicked element, so as I mentioned before, you can achieve it using this expression, which points to the currently clicked element. 如果我对您的理解正确,则希望获得clicked元素的value ,因此,正如我之前提到的,您可以使用this表达式来实现它, this表达式指向当前被单击的元素。

You should give a unique name to your inputs. 您应该给输入一个唯一的名称。 You cannot reuse id="user" multiple times or javascript will not be able to find each input element. 您无法多次重用id =“ user”,否则javascript将无法找到每个输入元素。

The following would be better: 以下将更好:

<?php
    $id = 0;
    foreach($query as $row) {
        $id += 1;
?>
<input type="text" name="user" id="user-<?php echo "$i"; ?>" value="<?php echo $row->users;?> onclick="select_online(<?php echo "$i"; ?>)">
<?php
    }
?>

<script>
    // Move script outside loop
    function select_online(i) {
        var user = document.getElementById("user-" + i).value;
        document.getElementById("usersonline").value = user;
    }
</script>

Name:<input type="text" name="usersonline"id="usersonline">

As per the HTML standard, there should be unique id in a document. 根据HTML标准,文档中应该有唯一的ID。 But in your case you are generating multiple input tags with id = user . 但是在您的情况下,您正在生成id = user多个输入标签。 Javascript's document.getElementById is able to get only element with id = user . Javascript的document.getElementById只能获取id = user元素。

For this you can change your code like this: 为此,您可以像这样更改代码:

<?php foreach($query as $row) {  ?>
    <input type="text"   name="user"  id="user"   value="<?php echo $row->users;?>" onclick="select_online(this)">
<?php } ?>
<script> 
function select_online(elm) {
    var user=elm.value;
    document.getElementById("usersonline").value=user;
}
</script>
Name:<input type="text" name="usersonline"id="usersonline">

Here onclick, we are passing the refferance of the input tag. 在点击这里,我们传递了输入标签的引用。 So we can get the refferance of the clicked input. 这样我们就可以得到点击输入的参考值。

 <input type="text" name="user" id="user" value="<?php echo $row->users;?> onclick="select_online()">

You are setting id as user for all the users so when you use selector document.getElementById("user"), it always fetches the first row. 您正在将所有用户的id设置为user,因此当您使用选择器document.getElementById(“ user”)时,它始终会提取第一行。

Never use same id for more than one element. 切勿对多个元素使用相同的ID。 It will always cause bugs. 它将始终导致错误。

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

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