while循环不工作后的代码

时间:2012-11-12 11:35:42

标签: javascript smarty

<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模板文件中运行。该代码工作正常,但它不运行第三个最后一行,即警报(“你好”); {redelim}被忽略之后的任何事情。怎么办?


这是来自萤火虫的代码:

<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> 

1 个答案:

答案 0 :(得分: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;
}


虽然我的问题是Solved,但我仍然想知道为什么我之前的代码无法正常工作,因为它仍然可以正常工作。如果有人能指出问题,请这样做我会很感激。