JavaScript:为什么警报会改变行为?

时间:2011-01-31 22:18:29

标签: javascript html

我有JavaScript函数,我在其中检查stageId框的值,如果它的值为2,则应激活Reason框并且应该是必需的,这意味着*应该出现在{{{}之前1}},这个函数运行正常但是{Box}只是暂时出现Reason box然后消失了,我暂时意味着很短的时间,但是如果我在*中有一个警报那么{ {1}}出现并保持在acitvateReason

之前

JavaScript代码

*

HTML代码

Reason Box

我无法理解为什么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=""; } } 只是在警报不存在的情况下暂时出现,如果警报存在<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方法中提出警告,有什么建议吗?

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

javascript中的某些函数会阻止代码继续(这称为blocking)。

在允许任何代码继续之前,

alertconfirm都会等待用户输入。 confirm执行此操作,因为它需要返回一个值。 alert用于调试目的。

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

我还建议在调用函数之前检查控制台是否存在,以便在不处于调试模式时不会抛出意外错误:if (window.console) console.log('some value');这对于调试对象非常有用,因为您将能够看到财产崩溃。

答案 2 :(得分:0)

<强>答案:

暂时,我又添加了一个被调用的Id,并留下了星号,这里是代码,但它不是问题的解决方案,我不知道我在这做什么错,所以真的很感激一些指针或相同的指导。

更新了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,否则可能会出现一些奇怪的行为。