0

ページには複数の質問/回答セットがあります。各セットは、クラス「questionItem」の div にグループ化されます。各セットには常にanswers[idx].SetAnserという名前の入力があり、idxがセットのインデックスであるanswers[idx].Answerがある場合もあります。

解決する必要がある問題は、SetAnswer が 1 つの値で、Answer が別の値であるすべての質問セット div 要素を見つけることです。サンプルの html は次のとおりです。

<div class="questionItem format">
    <input name="answers[0].SetAnswer" type="hidden" value="0" />

    <select name="answers[0].Answer">
      <option selected="selected" value="0">(0) No</option>
      <option value="1">(1) Yes</option>
      <option value="-">(-) Not assessed</option>
    </select>
</div>

<div class="questionItem format">
    <input name="answers[1].SetAnswer" type="hidden" value="?" />
    <select name="answers[1].Answer">
        <option selected="selected" value="?">(select item)</option>
        <option value="0">(0) No</option>
        <option value="1">(1) Yes</option>
        <option value="-">(-) Not assessed</option>
    </select>
</div>

<div class="questionItem format">
    <input name="answers[2].SetAnswer" type="hidden" value="?" />
    <input name="answers[2].Answer" type="number" value="" />    
</div>

<div class="questionItem format">
    <input name="answers[3].SetAnswer" type="hidden" value="red" />
    <input name="answers[3].Answer" type="number" value="red" />    
</div>

私が思いついた最高のものは、次のようなものです。

var answerColl = $(this).find(":input").filter(function () {
    return /^answers\[\d+\]\.Answer$/.test(this.name); 
    });

// Make sure there is an 'Answer' input, if there isn't, that is because it is 
// not displayed, so assume not unanswered
if (answerColl.length > 0) {
    var answerElement = answerColl.first();
    var answer = answerElement.value;

    // First check to see if the answer is ?
    if (answer == "?")
        return true;

    // Next check to see if the answer is empty, if so, must check the setAnswer.  
    // If the setAnswer is ^, then this is no unanswered, if it is ? it is unanswered.
    if (!Boolean(answer)) {

        var setAnswerElement = $(this).find(":input").filter(function () { return /^answers\[\d+\]\.SetAnswer$/.test(this.name); }).first();
        var setAnswer = setAnswerElement.value;

        if (setAnswer == "?")
            return true;
    }
}

return false;

問題は次のとおりです。

var answerElement = answerColl.first();
var answer = answerElement.value;

デバッガーで見ると、answerElement は jQuery オブジェクトですが、answer は常に null です。answerElement を見ると、デフォルト値は私が探している値、実際の答えですが、コードでそれを取得する方法がわかりません。

助言がありますか?

4

1 に答える 1

0

これについて正規表現を記述する必要はないと思います.. $.each ループを使用するだけでこれを簡単に解決できます.. 最初に要素を選択します.. 次に、それらの要素に対して $.each ループを実行して確認しますあなたが述べた条件のために..これを試してください

$(function() {

    $('#btn1').on('click', function() {
        var correctAnswer = $('.questionItem input');
        var userAnswer = $('.questionItem select option:selected');

        $.each(correctAnswer, function(i) {
            // This will give the Question Div's Whose SetAnswer and Answers are different..
            if ($(this).val() != $(userAnswer[i]).val()) {

                // Will give a List of all Answers that are set
                console.log('Correct Answer for Question ' + i + ' is  : ' + $(this).val());
                // Will give a List of all Answers that the user Answered
                console.log('User Selection for Question ' + i + ' is  : ' + $(userAnswer[i]).val());

                // This will print the Question Div for Which 
                // SetAnswer and Answer is different..
                console.log('SetAnswer is of one value and Answer is of another value for : ' + $(this).parent().attr('id'));
            }
        });
    });

});​

実際の例については、このFIDDLEを確認してください。

于 2012-09-07T20:01:30.113 に答える