2011-01-31 39 views
0

我有javascript函数中,我查了级ID框的值,如果其价值2那么原因箱应被激活,它应该是必需的,这意味着*应该出现提前Reason box的,此功能工作正常,但*领先理性对话框出现只是暂时,然后消失,通过瞬间我的意思是很短的时间量,但如果我在acitvateReason有一个警报,然后*出现并保持领先的Reason BoxJavaScript:为什么警报会使行为有所不同?

JavaScript代码

function activateReason(){ 
    alert('test'); 
    var stageId = $('#Main\\.stageId option:selected').val(); 
    if(stageId== '2') { 
    document.getElementById("Main.ReasonId").disabled=false; 
    document.getElementById("rqd.Main.ReasonId").style.display='inline'; 
    }else{ 
    document.getElementById("Main.ReasonId").disabled=true; 
    document.getElementById("rqd.Main.ReasonId").style.display='none'; 
    document.getElementById("Main.ReasonId").selectedIndex=""; 
    } 
} 

HTML代码

<td width="15%" class="formLabel" align="right"> 
<form:label path="Main.ReasonId" cssClass="normalText" cssErrorClass="normalTextRed"> 
<span id="rqd.Main.ReasonId" style="display:none;"><sup><b>*</b></sup></span> 
Reject Reason: </form:label> 
</td> 
<td width="35%" class="formValue"> 
<form:select path="Main.ReasonId" cssClass="normalText" cssErrorClass="validationError" disabled="true"> 
<form:option value="" label="--Please Select--"/> 
<form:options items="${ReasonList}" itemValue="value" itemLabel="label"/> 
</form:select> 
</td> 

我无法理解,为什么*时,警报不存在似乎只是暂时,如果警报存在*停留,我想*留下来,我不知道的我怎么能这样做,也会很感激,如果有人可以解释这种行为?

更新:我的萤火检查,它看来,如果我没有戒备,activateReason()比显示document.getElementById("rqd.Main.ReasonId").style.display='inline';变化从行内首屈一指,如果我没有戒备比它仍然是内联等*出现。

不知道为什么会发生这种情况,我想保留*但我不想在activateReason方法中有警报,有什么建议吗?

+0

你在哪里把警报()在功能改变自己的行为? – Anonymoose 2011-01-31 22:21:45

+0

在函数中添加了警报声明。 – Rachel 2011-01-31 22:23:30

+0

其实没关系,我把警报,如果我把警报的任何地方enableRejectReason功能,那么``仍然存在,但如果我删除警报,然后`*`短暂停留,然后消失。 – Rachel 2011-01-31 22:24:50

回答

0

答:

对于暂时,我还增加了一个编号都会调用,使星号留下来,这里是代码,但它不是对问题的解决方案,我不知道自己做错了什么我在这里做,所以会真正感谢一些指针或相同的指导。

更新JavaScript代码

function activateReason(){ 
    alert('test'); 
    var stageId = $('#Main\\.stageId option:selected').val(); 
    if(stageId== '2') { 
    document.getElementById("Main.ReasonId").disabled=false; 
    document.getElementById("rqd.Main.ReasonIdFor2").style.display='inline'; 
    }else{ 
    document.getElementById("Main.ReasonId").disabled=true; 
    document.getElementById("rqd.Main.ReasonId").style.display='none'; 
    document.getElementById("rqd.Main.ReasonIdFor2").style.display='none'; 
    document.getElementById("Main.ReasonId").selectedIndex=""; 
    } 
} 

更新HTML代码:

<td width="15%" class="formLabel" align="right"> 
    <form:label path="Main.ReasonId" cssClass="normalText" cssErrorClass="normalTextRed"> 
     <span id="rqd.Main.ReasonId" style="display:none;"> 
     <sup><b>*</b></sup> 
     </span> 
     <span id="rqd.Main.ReasonIdFor2" style="display:none;"> 
     <sup><b>*</b></sup> 
     </span> 
     Reason: 
    </form:label> 
</td> 
<td width="35%" class="formValue"> 
    <form:select path="Main.ReasonId" cssClass="normalText" cssErrorClass="validationError" disabled="true"> 
    <form:option value="" label="--Please Select--"/> 
    <form:options items="${ReasonList}" itemValue="value" itemLabel="label"/> 
    </form:select> 
</td> 

再次,这仅仅是一个解决办法,但不是最终的解决方案,如果一些有用的建议,以应对将不胜感激提出这个问题。

观察 虽然使用JavaScript它始终是更好地对所有元素的唯一ID别的有一些怪异的行为的机会。

0

javascript中的某些函数停止继续执行代码(这称为blocking)。

alertconfirm都在任何代码被允许继续之前等待用户输入。 confirm会这样做,因为它需要返回一个值。 alert这样做是为了调试目的。

而不是使用alert我强烈建议使用console.logconsole.debugconsole.error代码调试(与萤火虫或其他任何浏览器的调试器)

我也建议检查控制台存在调用函数之前,所以,你不要在不处于调试模式时抛出意外错误:if (window.console) console.log('some value');这对于调试对象来说非常棒,因为您将能够看到属性分解。

1

最可能的解释是表单正在提交,因此页面正在重新加载。那个“时刻”是你的Javascript显示星号完成重载和重画屏幕的时间。

相关问题