- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
好的,这次代码让我抓狂
我花了 2 周多时间使用 e.preventDefault() 并返回 flase 以防止 onclick href 转到页面顶部,但它们没有用
我用过 javascript:void(0)
它停止将窗口滚动到顶部,但未生成输入字段。
我确定在我的代码中使用这些解决方案存在问题,如果在我的代码中使用这两种方法存在问题,请告诉我
我的代码:
我正在尝试使用 PHP 和 jquery 构建向导
在此向导的第 2 步中,我想在用户单击时动态生成输入字段
<h2><a href="#" id="addScnt">Add Another Input Box</a></h2>
这是Javascript
$(document).ready(function(){
/*****************************/
$('#table_name_error').hide();
// all content starts hidden
$('.wizardcontent').hide();
$('#wizardcontent').hide();
// initialize the wizard state
load_state(1);
// loads new state based on button clicked
$(':button').click(function(){
//reset the wizardcontent to hidden
//$('#wizardcontent').hide();
//load_state(++current_state);
var current_state = $('#wizard').attr('class');
//we only want the number, converted to an int
current_state = parseInt(current_state.replace(/(step_)/, ""));
//figure out if 'next' or 'previous' was clicked and load appropriate state
if ($(this).attr('id') == 'next'){
switch(current_state){
case 1:
$('#wizardcontent').hide();
load_state(++current_state);
break;
case 2:
var scntDiv = $('#p_scents');
var i = $('#p_scents p').size() + 1;
$('#addScnt').on('click', function() {
$('<p><label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt_' + i +'" value="" placeholder="Input Value" /></label> <a href="#" id="remScnt">Remove</a></p>').appendTo(scntDiv);
i++;
return false;
});
$('#remScnt').on('click', function() {
if( i > 2 ) {
$(this).parents('p').remove();
i--;
}
return false;
});
var table_name=$('#table1').val();
table_name=$.trim(table_name);
if (table_name.length<3 || table_name.length>64){
$('#table_name_error').show();
$('#table_name_error').html("<p>Error: Minimum length for table name is 3 <br/>Maximum length for table name is 64 characters</p>");
}else{
$('#wizardcontent').hide();
load_state(++current_state);
}
break;
case 3:
$('#wizardcontent').hide();
load_state(++current_state);
break;
}
}
else if ($(this).attr('id') == 'previous'){ load_state(--current_state); }
});
/**************************/
});
function load_state(current_state){
//disable all buttons while loading the state
$('#previous').attr("disabled","disabled");
$('#next').attr("disabled","disabled");
$('#save').hide();
//load the content for this state into the wizard content div and fade in
var content = $('#step_' + current_state).html();
$('#wizardcontent').html(content);
$('#wizardcontent').fadeIn("slow");
//set the wizard class to current state for next iteration
$('#wizard').attr('class','step_'+ current_state);
var iterator = 1;
// the state heading h3. removing is no biggie
$('#wizard h3').text("Step " + current_state);
// loop through the list items and set classes for css coloring
$('#mainNav li').each(function(){
var step = $(this)
if (iterator == current_state){ step.attr('class','current'); }
else if (current_state - iterator == 1){ step.attr('class','lastDone'); }
else if (current_state - iterator > 1){ step.attr('class','done'); }
else{ step.attr('class',''); }
// special case for step 5 because it doesn't have bacground image
if (iterator == 3){ step.addClass('mainNavNoBg'); }
iterator++;
});
//depending on the state, enable the correct buttons
switch(current_state){
case 1:
$('#next').show();
$('#next').removeAttr("disabled");
$('#previous').hide();
break;
case 3:
$('#previous').show();
$('#previous').removeAttr("disabled");
$('#next').hide();
$('#save').show();
break;
default:
$('#save').show();
$('#previous').show();
$('#next').show();
$('#previous').removeAttr("disabled");
$('#next').removeAttr("disabled");
break;
}
}
这是html代码
<fieldset class="wizardcontent" id="step_2">
<b>Type name of the first table:</b><br/>
<input type='text' id='table1' name='table1'/>
<div id='table_name_error' name='table_name_error' class='error'></div>
<br/><br/>
<b>Identify table attributes, along with the basic type:</b><br/>
<h2><a href="#" id="addScnt">Add Another Input Box</a></h2>
<div id="p_scents">
<p>
<label for="p_scnts"><input type="text" id="p_scnt" size="20" name="p_scnt" value="" placeholder="Input Value" /></label>
</p>
</div></fieldset>
最佳答案
.preventDefault();
我在你的代码中找不到 preventDefault();
但这可能是一个拼写错误,就像你的标题一样......需要确定一些事情。
确保你有事件变量:
$('a').click(function(event) {
// `event` is the event variable
});
然后只需使用:
event.preventDefault();
总而言之:
$('a').click(function(event) {
event.preventDefault();
});
以上代码将阻止所有 anchor 转到其指定的超链接。
返回错误;
我以前经常使用 return false;
,但最近我已经转移到 .preventDefault()
来处理事件。这里要记住的一件事是,如果包含 return false;
的函数的任何部分抛出异常,它将不会到达它。因此,您试图阻止的事件无论如何都会触发。
关于Jquery:.preventDefault() 并返回 false 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15584584/
class test { public static void main(String[] args){ Object o1 = new Object(); O
我以为我理解了 Python 中的这两个单例值,直到我看到有人在代码中使用 return l1 or l2,其中 l1 和 l2 都是链表对象,并且(s)他想如果不为 None 则返回 l1,否则返回
这个问题在这里已经有了答案: Why does the expression 0 >> (True == False) is False True >>> True == (False is Fals
为什么在 Python 中它是这样评估的: >>> False is False is False True 但是当用括号尝试时表现如预期: >>> (False is False) is False
我有一个名为“apple”的表,我编写了以下查询: select name, count(name), case when istasty is null then fal
python boolean 逻辑中的运算符优先级 print(False==True or False) #answer is True print(False==(False or True))#
请不要看条件,因为它们在这里是为了便于理解行为 为什么 result 等于 true ? boolean result = false && (false)?false:true; 我知道我们可以通过
乍一看,这篇文章可能看起来像是重复的,但事实并非如此。相信我,我已经查看了所有 Stack Overflow,但都无济于事。 无论如何,我从 Html.CheckBoxFor 得到了一些奇怪的行为。
这个问题在这里已经有了答案: python operator precedence of in and comparison (4 个答案) 关闭 6 年前。 我的一位前辈演示了它,我想知道这是否是
我最近参加了 Java 的入门测试,这个问题让我很困惑。完整的问题是: boolean b1 = true; boolean b2 = false; if (b2 != b1 != b2) S
为什么 {} == false 评估为 false 而 [] == false 评估为 true在 javascript 中? 最佳答案 这是根据 Abstract Equality Comparis
这个问题在这里已经有了答案: Why does (1 in [1,0] == True) evaluate to False? (1 个回答) 关闭7年前。 为什么使用括号时这些语句按预期工作: >>
我试过搜索这个,但我真的不知道如何表达它以查看是否有其他人发布了答案。 但是,我正在制作一个国际象棋游戏和一个人工智能来配合它,这是非常困难的,我的问题是当我检查两个棋子是否在同一个团队时我必须做 (
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
为什么 为 false || null 返回与 null || 不同的结果错误? 我可以安全地依赖 return myVar || false 如果 myVar 为 null 或 false,则返回
我正在尝试遵循 NHibernate 教程,“你的第一个基于 NHibernate 的应用程序:修订 #4”在 NHibernate Forge。 但线路:new SchemaExport(cfg).
这个问题在这里已经有了答案: Empty list boolean value (3 个答案) 关闭 4 年前。 我是 Python 的新手,不理解以下行为: 为什么要声明 [] == False
以下函数循环访问对象的值。如果值为空this.hasInvalidValue设置为true ,如果不为空 this.hasInvalidValue设置为false : user: { email:
所以我正在玩 java.lang.reflect 东西并尝试制作类似 this 的东西。这是我的问题(可能是一个错误): 将字段设置为 true 的方法的代码: private static void
当我在编程时,我的 if 语句出现了意想不到的结果。 这个代码警报怎么会是真的?我在 W3S 没有找到任何可以帮助我的东西,我真的很想知道为什么这些警报是“正确的” window.alert(fals
我是一名优秀的程序员,十分优秀!