gpt4 book ai didi

javascript - 未定义的输入、动态页面更改、html 中的 javascript

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

当我单击注册按钮并出现输入字段时出现问题。当我填写的时候,当代码跳转到function $(document).on("click", "#register_in", function()这个函数里面的所有$().val()都是undefined .我猜Dom树有问题。谁能帮帮我。

$(document).on("click", "#register_in", function() {
var error = "";
if ($("username").val() == "" || $("password").val() == "" || $("name").val() == "" ||
$("lastname").val() == "" || $("jmbg").val() == "" || $("tel").val() == "" || $("email").val() == "") {
error += "Sva polja moraju da budu popunjena";
}

var n = $("jmbg").length;
if (n != 13) {
error += "\nJMBG mora da ima 13 cifara";
}

$("#error1").text(error);
});



$("#register").click(function() {
var text = "<div class=\"Centar\"><div id= \"error1\" class=\"Red\" ></div >";
text += "Korisnicko ime: <br /> <input type=\"text\" id=\"username\" /> <br />";
text += "Lozinka: <br /> <input type=\"password\" id=\"password\" /> <br />";
text += "Ime:<br /> <input type=\"text\" id=\"name\" /> <br />";
text += "Prezime:<br /> <input type=\"text\" id=\"lastname\" /> <br />";
text += "Pol: <br /> <select id=\"pol\"><option value=\"M\">Musko</option><option value=\"Z\">Zensko</option></select><br />";
text += "JMBG:<br /> <input type=\"text\" id=\"jmbg\" /> <br />";
text += "Kontakt telefon:<br /> <input type=\"text\" id=\"tel\" /> <br />";
text += "Email:<br /> <input type=\"text\" id=\"email\" /> <br />";
text += "<input type=\"submit\" id=\"register_in\" value=\"Register\" /> </div >";
$("#screen").html(text);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div class="Naslov">Taksi sluzba</div>
<div id="mainmanu" class="Meni">
<table>
<tr>
<td name="menu" id="login">Login</td>
<td>|</td>
<td name="menu" id="register">Register</td>
</tr>
</table>
</div>

<br /><br /><br />
<div id="screen">
<div class="Centar">
<div id="error1" class="Red"></div>
Korisnicko ime:<br /> <input type="text" id="username" /><br /> Lozinka:
<br /> <input type="password" id="password" /><br />
<input type="submit" id="log_in" value="Login" />
</div>
</div>

最佳答案

你必须设置 #这里$("#...").val()因为它是 id 的选择器
看下面的代码:

$(document).on("click", "#register_in" , function () {
var error = "";
if ($("#username").val() == "" || $("#password").val() == "" || $("#name").val() == "" ||
$("#lastname").val() == "" || $("#jmbg").val() == "" || $("#tel").val() == "" || $("#email").val() == "")
{
error += "Sva polja moraju da budu popunjena";
}

var n = $("jmbg").length;
if (n != 13)
{
error += "\nJMBG mora da ima 13 cifara";
}

$("#error1").text(error);
});


$("#register").click(function () {
var text = "<div class=\"Centar\"><div id= \"error1\" class=\"Red\" ></div >";
text += "Korisnicko ime: <br /> <input type=\"text\" id=\"username\" /> <br />";
text += "Lozinka: <br /> <input type=\"password\" id=\"password\" /> <br />";
text += "Ime:<br /> <input type=\"text\" id=\"name\" /> <br />";
text += "Prezime:<br /> <input type=\"text\" id=\"lastname\" /> <br />";
text += "Pol: <br /> <select id=\"pol\"><option value=\"M\">Musko</option><option value=\"Z\">Zensko</option></select><br />";
text += "JMBG:<br /> <input type=\"text\" id=\"jmbg\" /> <br />";
text += "Kontakt telefon:<br /> <input type=\"text\" id=\"tel\" /> <br />";
text += "Email:<br /> <input type=\"text\" id=\"email\" /> <br />";
text += "<input type=\"submit\" id=\"register_in\" value=\"Register\" /> </div >";
$("#screen").html(text);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<div class="Naslov">Taksi sluzba</div>
<div id="mainmanu" class="Meni">
<table>
<tr>
<td name="menu" id="login">Login</td>
<td>|</td>
<td name="menu" id="register">Register</td>
</tr>
</table>
</div>

<br /><br /><br />
<div id="screen">
<div class="Centar">
<div id="error1" class="Red"></div>
Korisnicko ime:<br /> <input type="text" id="username" /><br />
Lozinka:<br /> <input type="password" id="password" /><br />
<input type="submit" id="log_in" value="Login" />
</div>
</div>
</body>

关于javascript - 未定义的输入、动态页面更改、html 中的 javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50782350/

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