[英]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.