使用 javascript codeigniter 更改文本框值

change textbox value with javascript codeigniter

我对 javascript 的看法:

<html>
<head>
<script type="text/javascript">
    document.getElementById("asd").value = 'sdasdsad';
</script>
</head>

<body>
$atta = array(
    'id' => 'asd'
);
echo form_input($atta);
</body>
</html>

结果是,我的文本框是空的。发生什么事了?

您没有将 php 脚本附在 php 标签中。

为什么不试试这个。

<html>
<head>
<script type="text/javascript">
    window.onload=function() {
       document.getElementById("asd").value = 'sdasdsad';
    }
</script>
</head>

<body>
<input type="text" id="asd" >
</body>
</html>

问题是您的 JavaScript 在元素呈现之前是 运行。做这样的事情:

<html>
<head>
<script type="text/javascript">
    window.onload = function() {
        document.getElementById("asd").value = 'sdasdsad';
    }
</script>
</head>

<body>
<?php
$atta = array(
    'id' => 'asd'
);
echo form_input($atta);
?>
</body>
</html>