2012-06-29 77 views
0

我正在開發評估系統。頁面上有很多'questionItem'塊。目標是確定答案[2] .SetAnswer或answers [2] .Answer被設置爲空字符串還是?並高亮標題(項目)。當用戶更改問題時,請刪除高亮顯示。使用jQuery來查找特定輸入

它的工作原理,我只是尋找我可以改進它。這裏是一個塊:

$<div class="questionItem"> 
    <input data-val="true" data-val-number="The field MappingId must be a number." data-val-required="The MappingId field is required." id="answers_2__MappingId" name="answers[2].MappingId" type="hidden" value="24" /> 
    <input id="answers_2__Item" name="answers[2].Item" type="hidden" value="B0300" /> 
    <input id="answers_2__OldAnswer" name="answers[2].OldAnswer" type="hidden" value="^" /> 
    <input id="answers_2__SetAnswer" name="answers[2].SetAnswer" type="hidden" value="^" /> 
    <h3 class="item">B0300</h3> 
    Hearing aid<br/> 
    <select class="hyperSwap" id="answers_2__Answer" name="answers[2].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"> 
    <input data-val="true" data-val-number="The field MappingId must be a number." data-val-required="The MappingId field is required." id="answers_11__MappingId" name="answers[11].MappingId" type="hidden" value="228" /> 
    <input id="answers_11__Item" name="answers[11].Item" type="hidden" value="C0900A" /> 
    <input id="answers_11__OldAnswer" name="answers[11].OldAnswer" type="hidden" value="0" /> 
    <input id="answers_11__SetAnswer" name="answers[11].SetAnswer" type="hidden" value="?" /> 
    <h3 class="item">C0900A</h3> 
    Staff asmt mental status: recall current season <br/> 
    <div class="questionCheckbox"><input id="C0900A_0" name="answers[11].Answer" type="radio" value="0" /> (0) Not checked (No)</div> 
    <div class="questionCheckbox"><input id="C0900A_1" name="answers[11].Answer" type="radio" value="1" /> (1) Checked (Yes)</div> 
    <div class="questionCheckbox"><input id="C0900A_-" name="answers[11].Answer" type="radio" value="-" /> (-) Not assessed</div> 
</div> 

這裏是我想出了jQuery代碼:

$function highlightUnanswered() { 
    var coll = $(".questionItem").filter(function() { 
     return $(this).find(":input").filter(function() { 
      return (/^answers\[\d+\]\.SetAnswer$/.test(this.name) && this.value == "?") || 
          ((/^answers\[\d+\]\.Answer$/.test(this.name) && this.value == "?") && 
          (/^answers\[\d+\]\.Answer$/.test(this.name) && this.value == "")); 
     }).length > 0; 
    }); 

    coll.find(".item").addClass("unanswered"); 
    coll.find(":input").change(function (e) { $(this).parent().find(".item").removeClass("unanswered"); }); 
} 

有兩個問題:

Q1:最後一行,如果我不會誤認爲處理程序正被添加到所有的輸入中,包括隱藏的輸入。如何將它連接到答案[#]。回答輸入? Q2:這個表單上的某個人向我提供了第一個查詢,它提供了不同功能的初始集合。我明白它在做什麼,但我不明白如何:/^answers[\d+].Answer$/.test(this.name)是怎麼回事?爲什麼最後有一個.length()> 0?

回答

1

Q1:

coll.find(":input").filter(function(){ 
    return (/^answers\[\d+\]\.Answer$/.test(this.name); 
}).addClass('unanswered'); 

Q2:

http://api.jquery.com/filter/表示: 「對於每個元素,如果函數返回真(或一個 」truthy「 值)」,所以濾波器的功能可以簡單地返回.length :0是虛假的,其他數字是真理。

/^answers[\d+].Answer$/是一個正則表達式:

https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions

.test(this.name)檢查當前處理的輸入的名字所需的格式相匹配。

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp/test

相關問題