第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將表單上單擊的所有按鈕的值相加并插入文本

將表單上單擊的所有按鈕的值相加并插入文本

慕運(yùn)維8079593 2024-01-18 10:12:32
我有一張包含 6 個問題和 4 個按鈕答案的表格。每個按鈕都有一個不同的數(shù)值,我想總結(jié)一下。<form name="my-form" id="survey"><div id="formpage_1" class="question" style="visibility: visible; display: block;">    <h3>1. Question 1</h3>    <button class="btn" value="-15" name="price1">Strongly Disagree</button>    <button class="btn" value="-10" name="price1">Disagree</button>    <button class="btn" value="0" name="price1">Agree</button>    <button class="btn" value="0" name="price1">Strongly Agree</button>    <br>    <div class="change">    <input type="button" value="Next" onclick="pagechange(1,2);">    </div></div>根據(jù)該總和,我將在 div 中插入一些不同的文本。const myForm = document.forms["my-form"],toleR = document.getElementById("tolerance");myForm.onsubmit = (e) => {  e.preventDefault(); // disable form submit  //Count the value of each answer  let sum =    Number(myForm.price1.value) +    Number(myForm.price2.value) +    Number(myForm.price3.value) +    Number(myForm.price4.value) +    Number(myForm.price5.value) +    Number(myForm.price6.value);  //insert image and text  if (sum < 0)    txt = '<div><p>Profile A</p></div>';  if (sum < 12)    txt = '<div><p>Profile B</p></div>';  if (sum > 11)    txt = '<div><p>Profile C</p></div>';  if (sum > 17)    txt = '<div><p>Profile D</p></div>';  if (sum > 20)    txt = '<div><p>Profile E</p></div>';  myForm.totalSum.value = sum;  toleR.innerHTML = txt;};當(dāng)答案是單選按鈕時,我可以使用此功能,但我無法像我想要的那樣輕松地設(shè)置單選輸入的樣式,因此轉(zhuǎn)移到了按鈕?,F(xiàn)在我不確定為什么它停止工作了。獎勵積分:每個按鈕在單擊/“活動”時都保持橙色。幫助?工作示例: https: //jsfiddle.net/bnedale/jp0k18wd/7/
查看完整描述

3 回答

?
拉莫斯之舞

TA貢獻(xiàn)1820條經(jīng)驗 獲得超10個贊

請檢查下面的代碼。我已經(jīng)通過評論提到了更改。Change 1, Change 2 etc。

如果您不熟悉,請參閱querySelectorAll,, 。getAttributeclosest

const myForm = document.forms["my-form"],

? toleR = document.getElementById("tolerance");


myForm.onsubmit = (e) => {

? e.preventDefault(); // disable form submit


? // Change 1

? // get selected buttons, button with active class inside myForm

? let activeBtn = myForm.querySelectorAll('.btn.active');


? //Count the value of each answer

? let sum = 0;

? for (var i = 0; i < activeBtn.length; i++) {

? ? // Change 2

? ? // in case of button you can not have .value to get value.

? ? // instead we can use button.getAttrinute("value") to get its value

? ? sum += Number(activeBtn[i].getAttribute("value"))

? }


? // Change 3

? // use if then else if for next conditions

? //insert image and text

? if (sum < 0)

? ? txt = '<div><p>Profile A</p></div>';

? else if (sum < 12)

? ? txt = '<div><p>Profile B</p></div>';

? else if (sum > 11)

? ? txt = '<div><p>Profile C</p></div>';

? else if (sum > 17)

? ? txt = '<div><p>Profile D</p></div>';

? else if (sum > 20)

? ? txt = '<div><p>Profile E</p></div>';


? myForm.totalSum.value = sum;


? toleR.innerHTML = txt;

};


//page change function

function pagechange(frompage, topage) {

? var page = document.getElementById("formpage_" + frompage);

? if (!page) return false;

? page.style.visibility = "hidden";

? page.style.display = "none";


? page = document.getElementById("formpage_" + topage);

? if (!page) return false;

? page.style.display = "block";

? page.style.visibility = "visible";


? return true;

}


//reset the form and scroll to top

document.getElementById("secondaryButton").onclick = function() {

? pagechange(7, 1);

? document.body.scrollTop = 0; // For Safari

? document.documentElement.scrollTop = 0; // For Chrome, Firefox, IE and Opera

};


//// Get the container element

//var btnContainer = document.getElementById("investments");


// Change 4

// Get all buttons with class="btn" inside the entire form

var btns = document.getElementsByClassName("btn");


// Loop through the buttons and add the active class to the current/clicked button

for (var i = 0; i < btns.length; i++) {

? btns[i].addEventListener("click", function() {

? ? // Change 5

? ? // get active button in current question div.

? ? // this.closest(.question) will find parent with class question for current element

? ? // then find button with active class

? ? var current = this.closest('.question').getElementsByClassName("active");

? ? // If there's no active class

? ? if (current.length > 0) {

? ? ? current[0].className = current[0].className.replace(" active", "");

? ? }


? ? // Add the active class to the current/clicked button

? ? this.className += " active";

? });

}

.btn {

? border: 2px solid #ea730b;

? border-radius: 5px;

? padding: 10px;

? width: 20%;

? display: inline;

? float: left;

? margin: 10px;

? background-color: white;

}


.active,

.btn:hover {

? color: white;

? background-color: #ea730b;

? border: 2px solid #ea730b;

}


#myDIV {

? width: 80%;

? margin: auto !important;

}


#reset {

? float: left;

? clear: both;

}


.change {

? display: block;

? clear: left;

? padding: 20px 0 0 0;

? width: 20%;

? height: auto;

? margin: auto;

}

<form name="my-form" id="survey">

? <div id="formpage_1" class="question" style="visibility: visible; display: block;">

? ? <h3>1. Question 1</h3>

? ? <button class="btn" value="-15" name="price1">Strongly Disagree</button>

? ? <button class="btn" value="-10" name="price1">Disagree</button>

? ? <button class="btn" value="0" name="price1">Agree</button>

? ? <button class="btn" value="0" name="price1">Strongly Agree</button>

? ? <br>

? ? <div class="change">

? ? ? <input type="button" value="Next" onclick="pagechange(1,2);">

? ? </div>


? </div>


? <div id="formpage_2" class="question" style="visibility: hidden; display: none;">

? ? <h3>2. Question 2 </h3>

? ? <button class="btn" value="1" name="price2">Strongly Disagree</button>

? ? <button class="btn" value="2" name="price2">Disagree</button>

? ? <button class="btn" value="3" name="price2">Agree</button>

? ? <button class="btn" value="5" name="price2">Strongly Agree</button>

? ? <br>

? ? <div class="change">

? ? ? <input type="button" value="Back" onclick="pagechange(2,1);">

? ? ? <p style="display: inline;">&nbsp&nbsp&nbsp</p>

? ? ? <input type="button" value="Next" onclick="pagechange(2,3);">

? ? </div>

? </div>


? <div id="formpage_3" class="question" style="visibility: hidden; display: none;">

? ? <h3>3. Question 3</h3>

? ? <button class="btn" value="5" name="price3">Strongly Disagree</button>

? ? <button class="btn" value="3" name="price3">Disagree</button>

? ? <button class="btn" value="2" name="price3">Agree</button>

? ? <button class="btn" value="1" name="price3">Strongly Agree</button>

? ? <br>

? ? <div class="change">

? ? ? <input type="button" value="Back" onclick="pagechange(3,2);">

? ? ? <p style="display: inline;">&nbsp&nbsp&nbsp</p>

? ? ? <input type="button" value="Next" onclick="pagechange(3,4);">

? ? </div>

? </div>


? <div id="formpage_4" class="question" style="visibility: hidden; display: none;">

? ? <h3>4. Question 4</h3>

? ? <button class="btn" value="5" name="price4">Strongly Disagree</button>

? ? <button class="btn" value="3" name="price4">Disagree</button>

? ? <button class="btn" value="2" name="price4">Agree</button>

? ? <button class="btn" value="1" name="price4">Strongly Agree</button>

? ? <br>

? ? <div class="change">

? ? ? <input type="button" value="Back" onclick="pagechange(4,3);">

? ? ? <p style="display: inline;">&nbsp&nbsp&nbsp</p>

? ? ? <input type="button" value="Next" onclick="pagechange(4,5);">

? ? </div>

? </div>


? <div id="formpage_5" class="question" style="visibility: hidden; display: none;">

? ? <h3>5. Question 5</h3>

? ? <button class="btn" value="1" name="price5">Strongly Disagree</button>

? ? <button class="btn" value="2" name="price5">Disagree</button>

? ? <button class="btn" value="3" name="price5">Agree</button>

? ? <button class="btn" value="5" name="price5">Strongly Agree</button>

? ? <br>

? ? <div class="change">

? ? ? <input type="button" value="Back" onclick="pagechange(5,4);">

? ? ? <p style="display: inline;">&nbsp&nbsp&nbsp</p>

? ? ? <input type="button" value="Next" onclick="pagechange(5,6);">

? ? </div>

? </div>


? <div id="formpage_6" class="question" style="visibility: hidden; display: none;">

? ? <h3>6. Question 6</h3>

? ? <button class="btn" value="5" name="price6">Strongly Disagree</button>

? ? <button class="btn" value="3" name="price6">Disagree</button>

? ? <button class="btn" value="2" name="price6">Agree</button>

? ? <button class="btn" value="1" name="price6">Strongly Agree</button>

? ? <br>

? ? <div class="change">

? ? ? <input type="button" value="Back" onclick="pagechange(6,5);">

? ? ? <p style="display: inline;">&nbsp&nbsp&nbsp</p>

? ? ? <button type="submit" onclick="pagechange(6,7)">Calculate</button>

? ? </div>

? </div>


? <div id="formpage_7" style="visibility: hidden; display: none;">

? ? <div id="tolerance"></div>

? ? <br>

? ? <br>

? ? <input id="secondaryButton" type="reset" value="Start again">

? ? <input type="text" name="totalSum" value="" size="2" readonly="readonly">

? </div>

</form>


查看完整回答
反對 回復(fù) 2024-01-18
?
開心每一天1111

TA貢獻(xiàn)1836條經(jīng)驗 獲得超13個贊

您需要在表單中使用 type="button" 聲明按鈕,否則該按鈕將默認(rèn)用作“提交”


<button type="button" class="btn" value="-15" name="price1">Strongly Disagree</button>

      <button type="button" class="btn" value="-10" name="price1">Disagree</button>

      <button type="button" class="btn" value="0" name="price1">Agree</button>

      <button type="button" class="btn" value="0" name="price1">Strongly Agree</button>


查看完整回答
反對 回復(fù) 2024-01-18
?
隔江千里

TA貢獻(xiàn)1906條經(jīng)驗 獲得超10個贊

您的代碼中有一些錯誤

  1. 所有具有相同名稱的按鈕price1(它應(yīng)該是一個數(shù)組或者名稱應(yīng)該不同)

  2. 嘗試通過 id 獲取按鈕值

  3. 將按鈕的類型設(shè)置為button(type="button")

function pagechange(elem1,elem2){


const myForm = document.forms["my-form"],

  toleR = document.getElementById("tolerance");


myForm.onsubmit = (e) => {

  e.preventDefault(); // disable form submit


  //Count the value of each answer

  let sum =

    Number(document.getElementById("price1").value) +

    Number(document.getElementById("price2").value) +

    Number(document.getElementById("price3").value) +

    Number(document.getElementById("price4").value);


  //insert image and text

  

  if (sum < 0)

    txt = '<div><p>Profile A</p></div>';

  if (sum < 12)

    txt = '<div><p>Profile B</p></div>';

  if (sum > 11)

    txt = '<div><p>Profile C</p></div>';

  if (sum > 17)

    txt = '<div><p>Profile D</p></div>';

  if (sum > 20)

    txt = '<div><p>Profile E</p></div>';

alert(sum);

  myForm.totalSum.value = sum;


  toleR.innerHTML = txt;

};

}

 <form name="my-form" id="survey">

    <div id="formpage_1" class="question" style="visibility: visible; display: block;">

      <h3>1. Question 1</h3>

      <button class="btn" type="button" value="-15" name="price1" id="price1">Strongly Disagree</button>

      <button class="btn" type="button" value="-10" name="price1" id="price2">Disagree</button>

      <button class="btn" type="button" value="0" name="price1" id="price3">Agree</button>

      <button class="btn" type="button" value="0" name="price1" id="price4">Strongly Agree</button>

      <br>

      <div class="change">

        <input type="submit" value="Next" onclick="pagechange(1,2);">

      </div>


    </div>

    </form>


查看完整回答
反對 回復(fù) 2024-01-18
  • 3 回答
  • 0 關(guān)注
  • 240 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號