我練習一些jQuery和我工作的一個簡單的應用程序,要求用戶有什麼自己喜歡的食物和飲料的。當用戶選擇一個選項,導航或選擇相同的選項時,如何隱藏元素?
我試圖創建應用程序,以便生成的文本......「你最喜歡的食物是x和飲料爲y」,只能說明當用戶實際選擇自己喜歡的食物/飲料,當他們AREN不主動選擇。
我的問題是,我無法弄清楚如何隱藏生成的文本在用戶選擇的選項,因爲也有顯示文本一旦選擇由能力的方式。我使用了change()方法,它允許我在選擇新內容時顯示文本。但是如果用戶點擊或選擇相同的選項呢?
想法?
下面的代碼...
http://codepen.io/zharriott/pen/oZxvBO?editors=1011
$(document).ready(() => {
/******************
REFERENCES
******************/
const $foodSelector = $('.questions__q1'),
$drinkSelector = $('.questions__q2'),
$dropdownInputs = $('.questions select'),
$foodResultTxt = $('.result__fav-food'),
$drinkResultTxt = $('.result__fav-drink'),
$result = $('.result'),
$resultTxt = $result.text();
/******************/
$dropdownInputs.change(() => {
//IF food/drink has not been selected, leave result hidden
if ($foodSelector.find(':selected').text() === 'Select an option...' || $drinkSelector.find(':selected').text() === 'Select an option...') {}
else {
//Update and show result to display user's current answers
$foodResultTxt.text($foodSelector.find(':selected').text());
$drinkResultTxt.text($drinkSelector.find(':selected').text());
console.log($resultTxt);
$result.show(1400);
}
});
});
的思考?
試試? – Feathercrown