2017-03-21 13 views
0

フォームとしてクイズを作成しましたが、フォームがクリックされた後に結果が表示されます。それぞれの結果は値として出てきます。私はMarketoによって生成されたフォームに結果を入れる必要があります(marketoについての知識は今必要ではありません)。値はクリック後に表示する必要がありますが、値は表示されていますが、値は正しくありません。.selectedIndexのフォームが正しく入力されない問題

何らかの理由で値に対応する正しいインデックスを呼び出すことができません。どんな助け?

フォームコード:

<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) { 
      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>') ; 
    } 

作品、助けのための感謝:これにより

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(); 
    } 
} 
} 

:だから、私はこれを置き換えます!

+0

リンターのようなリンターを使用すると、すぐに小さなエラーを発見するのに役立ちます。 –

+0

また、 'for'ループの中で変数に代入しています。値は最後の反復の値になります。とにかく関数は同じなので、 'for'ループはその目的がありません。 –

答えて

0

ちょうどselectedIndexを台無しに比べ<select>valueを設定する方がはるかに簡単です:

select.value = 'Customer'; 

我々はそれに取り組んでいる一方で、あなたのすべての機能はかなり正確に同じことを行う - それは次のようになりより良い指標を受け入れ、閉鎖にインデックスが含まれている別の関数を返します。このことが一つの機能持っている:

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

もう一つは、割り当ては、比較と同じではありません。

if (grading[i].value = customer) { 

customerが偽でない限り、代入文は設定されている値を返すため、常にtrueを返します。比較には少なくとも==を使用する必要がありますが、値と値の型が等しいかどうかを確認するには、===を使用する方がはるかに優れています。

+0

うーん、私は試してみることができます。私は値を顧客のものであり、継続性やコンプライアンスではないことを検証していたので、私はその値を小文字に設定しました。クイズが値を設定するため値を設定できないので、勝者がコンプライアンスであることをコードに伝える方法がわからない場合、コンプライアンスはの

+0

selectIndexを取り除き、私は==を使用しましたが、if()セクションの値を囲む必要があったため、何も起こっていなかったのです。ご協力いただきありがとうございます!! – Libscribs

+0

問題はありません。喜んで助けてください:-) –

0

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>') ; 
    } 

おかげだから、私はこれを置き換えます!

関連する問題