- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在codepen.io中发现这段代码非常简单,我真的没有任何错误,代码工作得很好。
我希望能够删除标记中的 javascript 属性,并能够添加另一种类型的控件来继续或返回表单的步骤。
然后删除它:
<a href="#" onclick="show_step('1')">back</a>
<a href="#" onclick="show_step('3')">next</a>
并以这种方式替换为控件:
<a href="#back" class="btn btn-warning back">back</a>
<a href="#next" class="btn btn-success next">next</a>
从而消除这些易于从浏览器控制台修改的属性。
onclick="show_step('1')"
let currentStep = 1; // Variable that indicates the current step
function show_step(step) {
var data = $("#form").serialize();
var url = 'saveTemp.php?step=' + step;
var valid = true;
// [OPTIONAL] We validate only if it is going forward
if (currentStep < step) {
// We search all input fields within the current step.
$('#step' + currentStep).find('input').each((idx, el) => {
$field = $(el);
// If the field is empty
if (!$field.val()) {
$field.parent().addClass('error');
valid = false;
} else {
$field.parent().removeClass('error');
}
});
}
// If at least one field was not completed
if (!valid) {
return;
}
$('#step' + currentStep).css("display", "none");
$('#step' + step).fadeIn("slow");
currentStep = step;
};
$(function() {
$('#step' + currentStep).fadeIn("slow");
});
label {
display: block;
}
.step {
display: none;
}
.errorMsg {
display: none;
color: red;
}
.error .errorMsg {
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form id="form">
<div id="step1" class="step">
<h1>Step 1</h1>
<label>
<span>Name :</span>
<input name="name" />
<div class="errorMsg">You must enter your name</div>
</label>
<label>
<span>last name:</span>
<input name="last_name" />
<div class="errorMsg">You must enter your last name</div>
</label>
<!-- &&& -->
<a href="#" onclick="show_step('2')">next</a>
</div>
<div id="step2" class="step">
<h1>Step 2</h1>
<label>
<span>Email :</span>
<input name="email" />
<div class="errorMsg">You must enter your email</div>
</label>
<!-- &&& -->
<a href="#" onclick="show_step('1')">back</a>
<a href="#" onclick="show_step('3')">next</a>
</div>
<div id="step3" class="step">
<h1>Step 3</h1>
<a href="#" onclick="show_step('2')">back</a>
<a href="#" onclick="show_step('4')">next</a>
</div>
<div id="step4" class="step">
<h1>Step 4</h1>
<a href="#" onclick="show_step('3')">back</a>
</div>
</form>
最佳答案
OP 目标是使该过程独立于简单的数字索引(虽然有一个计数器,但它是针对 url
变量的)。 演示 2 依赖于 .next()
和.prev()
移动方法,有一个对象文字来存储它的数据,并且严重依赖 this
在运行时定位位置和状态。 演示 2 有更多“移动部件”,因此用户更难通过控制台进行操作。
on()
方法将点击事件委托(delegate)给所有
<a>
anchor 。
let currentStep = 0; // Variable that indicates the current step
$('#step' + currentStep).fadeIn("slow");
$('a.next, a.back').on('click', function(e) {
var step = $(this).data('idx');
xStep(step);
});
function xStep(step) {
var data = $("#form").serialize();
var url = 'saveTemp.php?step=' + step;
var valid = true;
// [OPTIONAL] We validate only if it is going forward
if (currentStep < step) {
// We search all input fields within the current step.
$('#step' + currentStep + ' .data').each(function(idx, el) {
// If the field is empty
if (!$(this).val()) {
$(this).next('.msg').addClass('error');
valid = false;
} else {
$(this).next('.msg').removeClass('error');
}
});
if (!valid) {
return
}
}
$('#step' + currentStep).css("display", "none");
$('#step' + step).fadeIn("slow");
currentStep = step;
}
label {
display: block;
}
.step,
.msg {
display: none;
}
.msg.error {
display: inline-block;
color: red;
}
legend {
font: 600 3ch/1.2 Consolas
}
.dir {
font-size: 2.5ch;
text-decoration:none;
}
.next::after {
content: '\0a\25b6';
font-size: 2ex;
}
.back::before {
content: '\25c0\0a';
font-size: 2ex;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<form id="form">
<fieldset id="step0" class="step">
<legend>Step 1</legend>
<label>
First Name:
<input name="fName" class='data'>
<b class="msg">You must enter your name</b>
</label>
<label>
Last Name:
<input name="lName" class='data'>
<b class="msg">You must enter your last name</b>
</label>
<!-- &&& -->
<a href="#/" class='next dir' data-idx='1'>next</a>
</fieldset>
<fieldset id="step1" class="step">
<legend>Step 2</legend>
<label>
Email:
<input name="email" type='email' class='data'>
<b class="msg">You must enter your email</b>
</label>
<!-- &&& -->
<a href="#/" class='back dir' data-idx='0'>back</a>
<a href="#/" class='next dir' data-idx='2'>next</a>
</fieldset>
<fieldset id="step2" class="step">
<legend>Step 3</legend>
<a href="#/" class='back dir' data-idx='1'>back</a>
<a href="#/" class='next dir' data-idx='3'>next</a>
</fieldset>
<fieldset id="step3" class="step">
<legend>Step 4</legend>
<input type='submit'>
<a href="#/" class='back dir' data-idx='2'>back</a>
</fieldset>
</form>
// Object literal stores data
var move = {
step: 0,
next: true
};
// Initial step
$('.step:first').fadeIn("slow");
// A click event on any a.dir...
$('a.dir').on('click', function(e) {
/* Get the fieldset.step parent of this a.dir
|| if this a.dir has .back class...
*/
var $current = $(this).parent('.step');
if ($(this).hasClass('back')) {
//...set the move object properties accordingly
move.step -= 1;
move.next = false;
} else {
//...otherwise change the properties the other way
move.step += 1;
move.next = true;
}
/* Invoke call() function on xStep() function
|| to set 'this' as the clicked <a>nchor
*/
xStep.call(this, move, $current);
});
// Pass the move object and the current .step
function xStep(move, $current) {
var data = $("#form").serialize();
var url = 'saveTemp.php?step=' + move.step;
// if next property is true...
if (move.next) {
/* find all .data in current .step and do
|| the following on each of them:
*/
$current.find('.data').each(function(idx, el) {
// Find input.data proceeding b.msg
var $msg = $(this).next('.msg');
// if this input.data has no value...
if (!$(this).val()) {
//...display error meassage
$msg.addClass('error');
} else {
//...otherwise remove any error message
$msg.removeClass('error');
}
});
// if there are any b.msg.error, end the function
if ($('.error').length > 0) {
return;
}
// Remove current .step
$current.css("display", "none");
// fadeIn proceeding .step
$current.next('.step').fadeIn("slow");
} else {
//...otherwise remove current .step
$current.css("display", "none");
// fadeIn preceeding .step
$current.prev('.step').fadeIn("slow");
}
}
html,
body {
font: 400 1.5ch/1.2 Consolas;
font-variant: small-caps
}
label,
input {
font: inherit;
font-variant: normal
}
label {
display: block;
}
.step,
.msg {
display: none;
}
.msg.error {
display: inline-block;
color: red;
}
legend {
font-size: 2.5ch;
}
.dir {
display: inline-block;
text-decoration: none;
margin-top: 12px;
}
.next::after {
content: '\0a\25b6';
font-size: 2ex;
}
.back::before {
content: '\25c0\0a';
font-size: 2ex;
}
[type=submit] {
float: right
}
<form id="form">
<fieldset class="step">
<legend>Step 1</legend>
<label>
First Name:
<input name="fName" class='data'>
<b class="msg">First name is required</b>
</label>
<label>
Last Name:
<input name="lName" class='data'>
<b class="msg">Last name is required</b>
</label>
<!-- &&& -->
<a href="#/" class='next dir'>Next</a>
</fieldset>
<fieldset class="step">
<legend>Step 2</legend>
<label>
Email:
<input name="email" class='data'>
<b class="msg">Email: username@domain.com</b>
</label>
<!-- &&& -->
<a href="#/" class='back dir'>Back</a>
<a href="#/" class='next dir'>Next</a>
</fieldset>
<fieldset class="step">
<legend>Step 3</legend>
<a href="#/" class='back dir'>Back</a>
<a href="#/" class='next dir'>Next</a>
</fieldset>
<fieldset class="step">
<legend>Step 4</legend>
<a href="#/" class='back dir'>Back</a>
<input type='submit'>
</fieldset>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
关于jquery - 如何删除 <a> 标签的 javascript 属性并将其替换为其他 jQuery 属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48363206/
在带有 jQuery 的 CoffeeScript 中,以下语句有什么区别吗? jQuery ($) -> jQuery -> $ - > 最佳答案 第一个与其他两个不同,就像在纯 JavaScr
已关闭。这个问题是 off-topic 。目前不接受答案。 想要改进这个问题吗? Update the question所以它是on-topic用于堆栈溢出。 已关闭13 年前。 Improve th
就目前情况而言,这个问题不太适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、民意调查或扩展讨论。如果您觉得这个问题可以改进并可能重新开放,visit
这个问题可能听起来很愚蠢,但请耐心等待,因为我完全是初学者。我下载了两个 jQuery 版本,开发版本和生产版本。我想知道作为学习 jQuery 的初学者,什么更适合我。 最佳答案 如果您对 jQue
The documentation说要使用 1.6.4,但我们现在已经升级到 1.7.2。 我可以在 jQuery Mobile 中使用最新版本的 jQuery 吗? 最佳答案 您当然可以,但如果您想
我在这里看到这个不错的 jquery 插件:prettyphoto jquery lightbox有没有办法只用一个简单的jquery来实现这样的效果。 我只需要弹出和内联内容。你的回复有很大帮助。
很明显我正在尝试做一些 jQuery 不喜欢的事情。 我正在使用 javascript 上传图片。每次上传图片时,我都希望它可见,并附加一个有效的删除脚本。显示工作正常,删除则不然,因为当我用 fir
这两个哪个是正确的? jQuery('someclass').click(function() { alert("I've been clicked!"); }); 或 jQuery('somec
我正在寻找一个具有以下格式的插件 if (jQuery)(function ($) { -- plugin code -- })(jQuery); 我明白 (function ($)
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭10 年前。 Improv
我知道这个问题已经被问过几次了,但想知道您是否可以帮助我解决这个问题。 背景:我尝试创建一个使用 Ajax 提交的表单(jQuery 表单提交)。我已经工作得很好,然后我想在表单上得到验证。我可以使用
我正在使用无处不在的jquery validate plugin用于表单验证。它支持使用metadata plugin用于向表单元素添加验证规则。 我正在使用此功能。当验证查找这些规则时,它会对元素进
我更喜欢为我一直在开发的网络社区添加实用的视觉效果,但随着事情开始堆积,我担心加载时间。 拥有用户真的更快吗加载(希望是缓存的)副本来自 Google 存储库的 jquery? 是否使用 jQuery
这个问题已经有答案了: Slide right to left? (17 个回答) 已关闭 9 年前。 你能告诉我有没有办法在 jQuery 中左右滑动而不使用 jQuery UI 和 jQuery
我如何找出最适合某种情况的方法?任何人都可以提供一些示例来了解功能和性能方面的差异吗? 最佳答案 XMLHttpRequest 是原始浏览器对象,jQuery 将其包装成一种更有用和简化的形式以及跨浏
运行时 php bin/console oro:assets:build ,我有 11 个这样的错误: ERROR in ../node_modules/jquery-form/src/jquery.
我试图找到 jQuery.ajax() 在源代码中的定义位置。但是,使用 grep 似乎不起作用。 在哪里? 谢谢。 > grep jQuery.ajax src/* src/ajax.js:// B
$.fn.sortByDepth = function() { var ar = []; var result = $([]); $(this).each(function()
我的页面上有多个图像。为了检测损坏的图像,我使用了在 SO 上找到的这个。 $('.imgRot').one('error',function(){ $(this).attr('src','b
我在理解 $ 符号作为 jQuery 函数的别名时遇到了一些麻烦,尤其是在插件中。你能解释一下 jQuery 如何实现这种别名:它如何定义 '$' 作为 jQuery 函数的别名?这是第一个问题。 其
我是一名优秀的程序员,十分优秀!