gpt4 book ai didi

javascript - 具有多个 div 的页面验证

转载 作者:行者123 更新时间:2023-12-02 16:33:32 25 4
gpt4 key购买 nike

我试图在有两个 div 的页面上进行验证

<form>
<div id="first>
<input type="text" name="name"/>
<button id="next">Next</button>
</div>
<div id="second" style="display:none">
<input type="text" name="phone">
<button id="finish">Finish</button>
<button id="back">Back</button>
</div>
</form>

因此,当我单击#next 时,它会显示第二个 div,但我只想检查该 div 的验证。

谢谢

最佳答案

既然您已经在标签中包含了 jQuery,那么您绝对应该坚持使用 jQuery 插件。这可以为您节省很多不必要的工作。

一些表单向导插件:

如果您确实需要自定义表单向导,您可以从这样的内容开始(根据您的 HTML 示例):

JSFiddle

<form method="post">
<div id="first">
<input id="name" type="text" name="name"/>
<button id="next">Next</button>
</div>
<div id="second" style="display:none;">
<input id="phone" type="text" name="phone">
<button id="finish">Finish</button>
<button id="back">Back</button>
</div>
</form>

<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.2.min.js"></script>

<script>
$(document).ready(function () {

$('#next').click(function (e) {
e.preventDefault();
if($('#name').val() == ''){
return alert('Type name!');
}
$('#second').show();
$('#first').hide();
});

$('#finish').click(function (e) {
e.preventDefault();
alert('hello ' + $('#name').val() + '! \nYour phone nr. is: ' + $('#phone').val());
});

$('#back').click(function (e) {
e.preventDefault();
$('#first').show();
$('#second').hide();
});

});
</script>

JSFiddle

关于javascript - 具有多个 div 的页面验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28111878/

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