gpt4 book ai didi

php - IE8中JS动态创建元素添加占位符

转载 作者:行者123 更新时间:2023-11-28 09:07:34 25 4
gpt4 key购买 nike

我有以下代码来显示管理员创建、暂停帐户的表单

echo "<FORM name=\"admin\" action=\"\" enctype=\"multipart/form-data\">";
echo "<select onchange=\"ChangeType();\" name=\"action\" id=\"action\">";
echo "<option value=\"select\">--Select--</option>";
echo "<option value=\"create\">Create Account</option>";
echo "<option value=\"suspend\">Suspend Account</option>";
echo "</select><br><br>";
echo "<div id=\"data\">";
echo "</div>";
echo "<input type=\"button\" name=\"button\" onclick=\"AccountAction()\" value=\"Submit\">";
echo "<input type=\"reset\" id=\"reset\" value=\"Reset\" onclick=\"ResetForm();\">";
echo "</FORM>";

以及当用户调用 ChangeType() 时在 DOM 中动态创建元素的 javascript

function ChangeType()
{
var type = document.getElementById("action").options[document.getElementById("action").selectedIndex].value;
if(type=="select")
{
document.getElementById('data').innerHTML = "";
}
if(type=="create")
{
document.getElementById('data').innerHTML = "<input id=\"username\" placeholder=\"Username\" type=\"text\" name=\"username\"><br><br>";
document.getElementById('data').innerHTML += "<input id=\"password\" placeholder=\"Password\" type=\"password\" name=\"password\"><br><br>";
return;
}
if(type=="suspend")
{
document.getElementById('data').innerHTML = "<input id=\"username\" placeholder=\"Username\" type=\"text\" name=\"username\"><br><br>";
return;
}
}

代码工作正常,但是我无法获取动态创建的用户名和密码输入类型的占位符

我已经看过了 Adding jQuery placeholders to dynamically created form elements

但这并没有帮助,因为只有当我在文本区域内单击一次然后在外部单击时,占位符才会出现。

最佳答案

You have to do like this :

document.getElementById('data').setAttribute("placeholder", "placeholder value");

In your code :

function ChangeType() {
var type = document.getElementById("action").options[document.getElementById("action").selectedIndex].value;
if (type == "select") {
document.getElementById('data').innerHTML = "";
document.getElementById('data').setAttribute("placeholder", "placeholder value");
}
if (type == "create") {
document.getElementById('data').innerHTML = "<input id=\"username\" placeholder=\"Username\" type=\"text\" name=\"username\"><br><br>";
document.getElementById('data').innerHTML += "<input id=\"password\" placeholder=\"Password\" type=\"password\" name=\"password\"><br><br>";
document.getElementById('data').setAttribute("placeholder", "placeholder value");
return;
}
if (type == "suspend") {
document.getElementById('data').innerHTML = "<input id=\"username\" placeholder=\"Username\" type=\"text\" name=\"username\"><br><br>";
document.getElementById('data').setAttribute("placeholder", "placeholder value");
return;
}}

Using Jquery (browser compatible solution) :

$("#data").attr("placeholder", "placeholder value");

关于php - IE8中JS动态创建元素添加占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16784158/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com