三國(guó)紛爭(zhēng)
2021-04-14 13:15:45
我正在處理具有多個(gè)下拉選項(xiàng)的表單。我在下面的表格中將所有內(nèi)容簡(jiǎn)化為三個(gè)問(wèn)題,以說(shuō)明我的問(wèn)題。我已經(jīng)嘗試過(guò)此代碼(以及一些變體):function update_variables(el, standard_val, var_list) { standard_val.value = var_list[el.getElementsByTagName('option')[el.selectedIndex].value];}var select2 = document.getElementById('think_question'), hidden_answer = document.getElementsByName('thought_goes_here')[0];var why_options = { 'yes': '', 'no': 'Well, you tried.'};select2.addEventListener('change', function() { update_variables(select2, hidden_answer, why_options);});var sel_group_control = document.getElementById('follower_question');var sel_free_will = document.getElementById('think_question');sel_group_control.addEventListener('change', answer_bypass);function answer_bypass() { var user_choice = sel_group_control.value; if (user_choice === 'no') { sel_free_will.selectedIndex = 2; sel_free_will.style.backgroundColor = "#D3D3D3"; sel_free_will.disabled = true; }}<h2>Life Decisions</h2><form> Be exactly like everone else? <select id='follower_question'> <option disabled selected value> -- select an option -- </option> <option>yes</option> <option>no</option> </select> <br> Think for yourself? <select id='think_question'> <option disabled selected value> -- select an option -- </option> <option>yes</option> <option>no</option> </select> <br> Original thought:<input name="thought_goes_here" size="50" value="" id="your_thoughts"></form>如果將問(wèn)題2設(shè)置為“否”,則已知問(wèn)題3的答案,并在其中填寫回答。如果問(wèn)題1為“否”,則問(wèn)題2應(yīng)設(shè)置為“否”且為只讀。我希望在回答問(wèn)題1時(shí)選擇“否”時(shí),問(wèn)題3也將自動(dòng)更新,但該功能似乎被忽略了。
1 回答

寶慕林4294392
TA貢獻(xiàn)2021條經(jīng)驗(yàn) 獲得超8個(gè)贊
您需要注冊(cè)一個(gè)事件以觸發(fā)第二個(gè)選擇更改。
像這樣:
function answer_bypass() {
var user_choice = sel_group_control.value;
if (user_choice === 'no') {
sel_free_will.selectedIndex = 2;
sel_free_will.style.backgroundColor = "#D3D3D3";
sel_free_will.disabled = true;
// Create a new 'change' event
var event = new Event('change');
// Dispatch it.
select2.dispatchEvent(event);
}
}
添加回答
舉報(bào)
0/150
提交
取消