简体   繁体   English

document.getElementById().value 不工作

[英]document.getElementById().value is not Working

I have been workin' on a project about employees So there is some data in the mysql database.我一直在做一个关于员工的项目所以 mysql 数据库中有一些数据。 So to get that and put it into a html table I used php.因此,为了得到它并将其放入 html 表中,我使用了 php。

$sql = "SELECT * FROM Stock_Management.`staff_details`";
$result = mysqli_query($conn, $sql);

if(mysqli_num_rows($result)){
    while($row = mysqli_fetch_assoc($result)){
        echo "<tr>
                <td scope='row'>".$row['id']."</td>
                <td>".$row['fname']."</td>
                <td>".$row['lname']."</td>
                <td>".$row['phno']."</td>
                <td>".$row['employee_address']."</td>
                <td>".$row['join_date']."</td>
                <td><i class='far fa-edit btnedit' value=".$row['id']." name='btn-edit' id='btn-edit' onclick='edit()' style='color: green; cursor: pointer'></i></td>
                <td><i class='far fa-trash-alt btndelete' value='".$row['id']."' name='btn-delete' id='btn-delete' onclick='delete()' style='color: tomato; cursor: pointer'></i></td>
            </tr>";
    }
}

So if you see the last two td tags you can see I used font awesome and gave them values respectively with name and id Attributes.因此,如果您看到最后两个td标记,您会看到我使用了 font awesome 并分别为它们提供了nameid属性的values And also onclick event with a function.还有带有 function 的onclick事件。

echo "<script>
    function edit(){
        let data_id = document.getElementById('btn-edit');
        console.log(data_id.value);
    }
</script>";

But Every time I am Getting undefined in the console.但是每次我在控制台中都不undefined So what's Wrong with my code.那么我的代码有什么问题。 Please Help Me.!请帮我。! Please.请。

In order to get the value you could change js code to:为了获得值,您可以将 js 代码更改为:

function edit(){
    console.log(event.target.value)
}

By clicking it browser creates event which you can target to get clicked value.通过单击它,浏览器会创建事件,您可以将其作为目标来获取点击值。

also, you are missing "'" around value value=".$row['id']."此外,您在 value value=".$row['id']." 周围缺少“'”。 keep in mind that you should not have multiple occurrences of the same id on the page so targeting btn-edit will never work if you have multiple elements with this id.请记住,您不应该在页面上多次出现相同的 id,因此如果您有多个具有此 id 的元素,定位 btn-edit 将永远无法工作。

You should not use "" tag for a button您不应该为按钮使用“”标签

Change 'value' in your element to将元素中的“值”更改为

data-value

Change the javascript line 'console.log(data_id.value);'更改 javascript 行 'console.log(data_id.value);' to

console.log(data_id.dataset.value);

For more information on the use of js dataset visit https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes有关使用 js 数据集的更多信息,请访问https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

Also, the <i> element does not contain the value property.此外, <i>元素不包含value属性。

1.you can not add value and name to tag <i> ,you should use input tag, 2.if you use this, I hope you don't have any problem with that; 1.你不能给标签<i>添加值和名称,你应该使用输入标签,2.如果你使用这个,我希望你没有任何问题;

<label class='far fa-edit btnedit' onclick='edit()' style='color: green; cursor: pointer;font-size: 25px;'>
    <input type='hidden' id='btn-edit' name='btn_edit' value='".$row['id']."'/>
</label>


<script>
    function edit(){
        let data_id = document.getElementById('btn-edit');
        console.log(data_id.value);
    }
</script>

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

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