.selectedIndex错误填写表单的问题

时间:2017-03-21 19:41:58

标签: javascript arrays

我创建了一个测验作为表单,结果在单击表单后出现。每个结果都作为一个值出现。我需要将结果填充到Marketo生成的表单中(现在不需要对marketo的了解)。因此,点击后需要显示值,这很好,我显示了值,但这是不正确的值。

由于某种原因,无法调用与该值对应的正确索引。有什么帮助吗?

表格代码:

<select id="personaBucket" name="personaBucket" class="mktoField mktoHasWidth mktoRequired" style="width: 300px;">
<option value="">I need to work on...</option>
<option value="Customer">Customer</option>
<option value="Compliance">Compliance</option>
<option value="Continuity">Continuity</option>
</select>

JS(为测验结果指定值):

if (customer < continuity && customer < compliance) {
        result = customer;
       } else if (compliance < continuity && compliance < customer) {
        result = compliance;
       } else if (continuity < compliance && continuity < customer) {
        result = continuity;
       } else {
        result = continuity;
       }

    grading = [
       {score:customer,value:"customer",feedback:"You need to work on customer experience. Fill out the form below to learn more."},
       {score:compliance,value:"compliance",feedback:"You need to work on compliance. Fill out the form below to learn more."},
       {score:continuity,value:"continuity",feedback:"You need to work on continuity. Fill out the form below to learn more."}
       ];

JS(使值对应于测验结果):

var select = document.getElementById("personaBucket");
    var persona = "Persona is: ";
    var i;
    for (i = 0; i < select.length; i++) {
        persona=persona + "\n" + select.options[i].text + " has index: " + select.options[i].index;
    }
    console.log(persona);

    for (i = 0; i < select.length; i++) {
    var selectNone = function () {
        document.getElementById("personaBucket").selectedIndex = "0"; 
    };
    var selectCustomer = function () {
        document.getElementById("personaBucket").selectedIndex = "1"; 
    };
    var selectCompliance = function () {
        document.getElementById("personaBucket").selectedIndex = "2"; 
    };
    var selectContinuity = function () {
        document.getElementById("personaBucket").selectedIndex = "3";
    };
    }

    for(i=0; i<grading.length; i++) {
    if(result == grading[i].score) {
    if (grading[i].value = customer) { 
        selectCustomer ();
        }
    else if (grading[i].value = compliance) { 
        selectCompliance ();
    }
    else if (grading[i].value = continuity) { 
        selectContinuity ();
    }
    else { 
        selectContinuity ();
    }
}
}

即使我正在调用索引#

,索引也出错了

编辑:摆脱了selectIndex,但最大的问题是我没有在下面的值中加上“”,就像Rob在下面提到的那样,用= =替换=。所以,我取而代之:

for(i=0; i<grading.length; i++) {
    if(result == grading[i].score) {
    if (grading[i].value = customer) { 
        selectCustomer ();
        }
    else if (grading[i].value = compliance) { 
        selectCompliance ();
    }
    else if (grading[i].value = continuity) { 
        selectContinuity ();
    }
    else { 
        selectContinuity ();
    }
}
}

有了这个:

for(i=0; i<grading.length; i++) {
        if(result == grading[i].score) {
            personaVal = grading[i].value;
        }
    }

    if ( personaVal == "customer" ) {
    $('#personaBucket').children().remove().end().append('<option selected value="customer">Customer</option>') ;
    }
    if ( personaVal == "compliance" ) {
    $('#personaBucket').children().remove().end().append('<option selected value="compliance">Compliance</option>') ;
    }
    if ( personaVal == "continuity" ) {
    $('#personaBucket').children().remove().end().append('<option selected value="continuity">Continuity</option>') ;
    }

现在完美地工作,感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

设置value的{​​{1}}比干扰<select>要容易得多:

selectedIndex

虽然我们正在使用它,但所有函数都完全相同 - 最好让这个函数接受一个索引并返回另一个包含闭包中索引的函数:

select.value = 'Customer';

还有一件事,作业与比较不同:

function setSelectValue(val) {
   return function() {
      select.value = val;
   }
}
var selectContinuity = setSelectValue('Customer');
var selectCustomer = setSelectValue('Continuity');

除非if (grading[i].value = customer) { 为假,否则这将始终返回true,因为赋值语句返回正在设置的值。您至少应该使用customer进行比较,但最好使用==检查值的值和类型是否相等。

答案 1 :(得分:0)

摆脱了selectIndex,但最大的问题是我没有把#34;&#34;围绕我的下面的值,就像Rob提到的那样,用==替换=。所以,我取而代之:

int *(*func())[]

有了这个:

for(i=0; i<grading.length; i++) {
    if(result == grading[i].score) {
    if (grading[i].value = customer) { 
        selectCustomer ();
        }
    else if (grading[i].value = compliance) { 
        selectCompliance ();
    }
    else if (grading[i].value = continuity) { 
        selectContinuity ();
    }
    else { 
        selectContinuity ();
    }
}
}

感谢您的帮助!