2012-11-12 104 views
0
<script type="text/javascript"> 
function prepare_values() 
{ldelim} 
     var flag= true; 
     var i=0; 
    var total_questions = new Array(); 
    var total_answer = new Array(); 
     while(flag) 
      {ldelim} 
      var questions = document.getElementById("question_div_"+i); 
      if (questions == null) 
      {ldelim} 
       flag=false; 
       alert("Value of flag = "+flag); 
      {rdelim} 

      var answer = document.getElementById("answer_div_"+i); 
      total_questions[i]=questions.firstChild.innerHTML; 
      if(answer.firstChild.attributes['type'].value == 'text') 
       {ldelim} 
        total_answer[i]=answer.firstChild.nodeValue; 
       {rdelim} 

      if(answer.firstChild.attributes['type'].value == 'textarea') 
       {ldelim} 
        total_answer[i]=answer.firstChild.nodeValue; 
       {rdelim} 

      if(answer.firstChild.attributes['type'].value == 'radio_div') 
       {ldelim} 
         var radio_button_parent = document.getElementById(answer.firstChild.id); 
         var oRadio = document.getElementsByName(radio_button_parent.firstChild.name); 
         for(var k = 0; k < oRadio.length; k++) 
         {ldelim} 
          if(oRadio[k].checked) 
          {ldelim} 
          var radio_button_value = document.getElementById(oRadio[k].id); 
          alert("From inside of ever = "+radio_button_value.nextSibling.innerHTML); 
          total_answer[i]=radio_button_value.nextSibling.innerHTML; 
          {rdelim} 
         {rdelim} 


       {rdelim} 

    i=i+1; 
    {rdelim} 

    alert("hello"); 
{rdelim} 
</script> 

以上是我的JS代碼,它是在一個SMARTY模板文件中運行,點擊一個按鈕。代碼工作正常,但它不運行第三行,即alert(「Hello」); {redelim}之後的任何事情都被忽略了。該怎麼辦 ?代碼while循環後不工作


這裏距離螢火蟲代碼:

<script type="text/javascript"> 
function prepare_values() 
{ 
var flag= true; 
var i=0; 
var total_questions = new Array(); 
var total_answer = new Array(); 
while(flag) 
{ 
var questions = document.getElementById("question_div_"+i); 
if (questions == null) 
{ 
flag=false; 
alert("Value of flag = "+flag); 
} 
var answer = document.getElementById("answer_div_"+i); 
total_questions[i]=questions.firstChild.innerHTML; 
if(answer.firstChild.attributes['type'].value == 'text') 
{ 
total_answer[i]=answer.firstChild.nodeValue; 
} 
if(answer.firstChild.attributes['type'].value == 'textarea') 
{ 
total_answer[i]=answer.firstChild.nodeValue; 
} 
if(answer.firstChild.attributes['type'].value == 'radio_div') 
{ 
var radio_button_parent = document.getElementById(answer.firstChild.id); 
var oRadio = document.getElementsByName(radio_button_parent.firstChild.name); 
for(var k = 0; k < oRadio.length; k++) 
{ 
if(oRadio[k].checked) 
{ 
var radio_button_value = document.getElementById(oRadio[k].id); 
alert("From inside of ever = "+radio_button_value.nextSibling.innerHTML); 
total_answer[i]=radio_button_value.nextSibling.innerHTML; 
} 
} 
} 
i=i+1; 
} 
alert("hello"); 
} 
</script> 
+0

您是否嘗試過使用JavaScript調試器(Chrome中的F12,安裝了Firebug的Firefox中的F12,Internet Explorer中的F12等)?任何JavaScript錯誤/例外/等。 – J0HN

+0

是的,我嘗試過。沒有例外。 – Nagri

+0

這個問題可能並不聰明,你可以在渲染後粘貼實際在html中的代碼嗎? – Yoshi

回答

1

我不知道什麼是在代碼中的問題,因爲它被認爲爲什麼它不工作,但是這是我得到了我的代碼直。
這是新的工作代碼:

function prepare_values() 
{ 
    var flag= true; 
    var i=0; 
    var total_questions = new Array(); 
    var total_answer = new Array(); 
    while(flag) 
    { 
     var questions = document.getElementById("question_div_"+i); 
     if (questions == null) 
      { 
       break; 
      } 
     var answer = document.getElementById("answer_div_"+i); 
     total_questions[i]=questions.firstChild.innerHTML; 
     if(answer.firstChild.attributes['type'].value == 'text') 
      { 
       total_answer[i]=answer.firstChild.nodeValue; 
      } 
     if(answer.firstChild.attributes['type'].value == 'textarea') 
      { 
       total_answer[i]=answer.firstChild.nodeValue; 
      } 
     if(answer.firstChild.attributes['type'].value == 'radio_div') 
     { 
      var radio_button_parent = document.getElementById(answer.firstChild.id); 
      var oRadio = document.getElementsByName(radio_button_parent.firstChild.name); 
      for(var k = 0; k < oRadio.length; k++) 
       { 
        if(oRadio[k].checked) 
        { 
         var radio_button_value = document.getElementById(oRadio[k].id); 
         alert("From inside of ever = "+radio_button_value.nextSibling.innerHTML); 
         total_answer[i]=radio_button_value.nextSibling.innerHTML; 
        } 
       } 
     } 
    i=i+1; 
    } 

    alert("hello"); 
} 

我所做的一切就是我所取代

if (questions == null) 
{ 
    flag=false; 
    alert("Value of flag = "+flag); 
} 


if (questions == null) 
{ 
    break; 
} 


雖然我的問題就解決了我還是想要知道爲什麼我以前的代碼是n不工作,因爲它仍然假設工作正確。如果任何人都可以指出問題,請這樣做,我會很感激。

+0

現在似乎有點自我放縱,但是..我想提一提,你必須在某個時候「休息」。善待你自己搞清楚。 :{D – Labu

+0

@WillemLabu我不太瞭解英語,但我的評論有點貶義。我的問題錯了嗎? – Nagri

+0

根本沒有貶義。你很好地搞清楚了! :{D – Labu