1
的代碼如下:jQuery - 如何在輸入時輸入一個變量?
\t var risk = 0;
if ($('input').val()) {
\t risk += 1 ;
}
\t if (risk <= 2) {
\t \t $("#riskAssess").append("LOW RISK").css({"color": "green", "font-size": "25px"});
\t }
\t else if (risk <= 4) {
\t \t $("#riskAssess").append("MODERATE RISK").css({"color": "yellow", "font-size": "25px"});
\t }
\t else {
\t \t $("#riskAssess").append("HIGH RISK").css({"color": "red", "font-size": "25px"});
\t }
<div id="q2">
\t \t \t <h2>Answer the Following:</h2>
\t \t \t <div class="grouping"><p><label for="Name">What is your name?</label></p><input type="text" name="Name"/></div>
\t \t \t <p>Select your age range.</p>
\t \t \t \t <div id="age">
\t \t \t \t <label for="U13">Under 13</label> <input class="check" type="checkbox" id="U13" name="U13" />
\t \t \t \t <label for="13-17">13-17</label> <input class="check" type="checkbox" id="13-17" name="13-17" />
\t \t \t \t <label for="18-25">18-25</label> <input class="check" type="checkbox" id="18-25" name="18-25" />
\t \t \t \t <label for="26-34">26-34</label> <input class="check" type="checkbox" id="26-34" name="26-34" />
\t \t \t \t <label for="35-54">35-54</label> <input class="check" type="checkbox" id="35-54" name="35-54" />
\t \t \t \t <label for="55-64">13-17</label> <input class="check" type="checkbox" id="55-64" name="55-64" />
\t \t \t \t <label for="64+">Over 64</label> <input class="check" type="checkbox" id="64+" name="64+" />
\t \t \t \t </div>
\t \t \t \t
\t \t \t <div class="grouping"><p><label for="Occupation">What is your occupation?</label></p><input type="text" name="Occupation"/></div>
\t \t \t <div class="grouping"><p><label for="City">What city do you live in?</label></p><input type="text" name="City"/></div>
\t \t \t <div class="grouping"><p id="placeofbirth"><label for="pob">Where were you born?</label></p><input type="text" name="pob"/></div>
\t \t \t <div id="button2"><button id="finish">Finish</button></div>
\t \t \t
\t \t </div>
\t \t
\t \t <div id="qfinal">
\t \t \t <h2>You are at a <span id="riskAssess"></span></h2>
它應該增加一個用於每個輸入。現在它保持不變。
例如,當你回答「你叫什麼名字」,應該改變的風險形式0到1
你如何運行代碼?它需要位於事件監聽器中,以便在用戶輸入內容時運行。 – Barmar
你可以請包括你的HTML?你的問題的核心雖然是你只負載運行這個代碼。 –
輸入中每個字符的風險變量是否應該增加? –