繁体   English   中英

如何将value =“”添加到输入

[英]How to add value=“” to input

是否可以使用javascript将value标签添加到输入中?

例如,将输入格式更改为:

<input type="text" name="mail" id="mail">

至:

<input type="text" name="mail" id="mail" value="<?php echo $_SESSION['email'] ?>

编辑:

<?php
echo '
<script type="text/javascript">

document.getElementById("mail").setAttribute("value", "<?php echo $_SESSION["email"];?>");

</script>';
?>

是的,您可以使用javascript访问输入的值字段

document.getElementById('mail').value = "someemail@email.com";

使用setAttribute()来填充元素的属性。

 document.getElementById("mail").setAttribute("value", "<?php echo $_SESSION['email'];?>"); 
 <input type="text" name="mail" id="mail"> 

您不能在PHP字符串中使用<?php 请改用串联。

<?php
echo '
<script type="text/javascript">

document.getElementById("mail").setAttribute("value", "' . $_SESSION["email"] . '");

</script>';
?>

不确定要实现的目标,但是可以将PHP值另存为JS变量,然后从那里开始。 像这样吗

var email = '<?php echo $_SESSION['email'];?>';
document.getElementById('mail').value = email;

使用echo方法,返回元素输入。

$youvalue = "This is the value of the input"
echo '<input type="text" name="mail" id="mail" value="'.$yourvalue.'">';

再次使用方法echo ,返回脚本。

$youvalue = "This is the value of the input"
echo '<script>var x = document.createElement("input");x.type = "text";x.name = "mail";x.id = "mail";x.value = "'.$youvalue.'"; document.body.appendChild(x);</script>'

暂无
暂无

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

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