简体   繁体   English

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

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

Is there a possibility to add the value tag to an input with javascript? 是否可以使用javascript将value标签添加到输入中?

eg to change a input fild in a form from: 例如,将输入格式更改为:

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

to: 至:

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

EDIT: 编辑:

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

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

</script>';
?>

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

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

Use setAttribute() to fill in the attribute of an element. 使用setAttribute()来填充元素的属性。

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

You can't use <?php inside a PHP string. 您不能在PHP字符串中使用<?php Use concatenation instead. 请改用串联。

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

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

</script>';
?>

Not sure what you are trying to achieve but you could save PHP value as a JS variable and go from there. 不确定要实现的目标,但是可以将PHP值另存为JS变量,然后从那里开始。 Something like this? 像这样吗

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

Using the method echo , returning the element input. 使用echo方法,返回元素输入。

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

Using the method echo again, returning a script. 再次使用方法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