簡體   English   中英

無法將單選按鈕與文本對齊

[英]Unable to align the radio buttons with text

我打算將radio按鈕與文本並排對齊。 我設法使radio按鈕垂直垂直對齊,但是現在無法使radio按鈕和文本並排放置。

 var allQuestions = [{ question: "Who is the Prime Minister of the United Kingdom?", choices: ["David Cameron", "Gordon Brown", "Winston Churchill"], correctAnswer: 0 }, { question: "Who is the Prime Minister of India?", choices: ["Rahul Gandhi", "Arvind Kejrival", "Narendra Damodardas Modi"], correctAnswer: 2 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump", "Barack Obama", "Hilary Clinton"], correctAnswer: 1 }, { question: " Who averaged one patent for every three weeks of his life?", choices: ["Thomas Edison", "Tesla", "Einstein"], correctAnswer: 0 }, { question: "What continent is cut into two fairly equal halves by the Tropic of Capricorn?", choices: ["Africa", "South America", "Australia"], correctAnswer: 2 }, { question: "What explorer introduced pigs to North America?", choices: ["Christopher Columbus", "Galileo", "Mussorie"], correctAnswer: 0 }, { question: "What F-word is defined in physics as a “nuclear reaction in which nuclei combine to form more massive nuclei”? ", choices: ["Furnace", "Fusion", "Fission"], correctAnswer: 1 }, { question: "What was the first planet to be discovered using the telescope, in 1781?", choices: ["Uranus", "Jupiter", "Mars"], correctAnswer: 0 }, { question: "Who is the chief minister of West Bengal, India?", choices: ["Buddhadev Bhattacharya", "Jyoti Basu", "Mamta Banerjee"], correctAnswer: 2 }, { question: "Which is the fastest growing religion in the world?", choices: ["Islam", "Christianity", "Hinduism"], correctAnswer: 0 } /* { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 } */ ]; $(document).ready(function() { var currentquestion = 0; var correctAnswers = 0; $(function() { $("#progressbar").progressbar({ value: 0 }); }); $('#question').html(allQuestions[currentquestion].question); $('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>"); $('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>"); $('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>"); $("#next").click(function() { if ($("input[name=option]:checked").val() == allQuestions[currentquestion].correctAnswer) { correctAnswers++; }; currentquestion++; $(function() { $("#progressbar").progressbar({ value: currentquestion * (allQuestions.length), }); }); if (currentquestion < allQuestions.length) { $('#question').html(allQuestions[currentquestion].question); $('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>"); $('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>"); $('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>"); if (currentquestion == allQuestions.length - 1) { $('#next').html("Submit"); //$('#next').off("click"); $('#next').click(function() { // if($("input[name=option]:checked").val()==allQuestions[currentquestion].correctAnswer){ // correctAnswers++; // }; // alert(correctAnswers); alert("Your Score is " + correctAnswers + " out of " + currentquestion + " and your percentage is " + (correctAnswers * 100 / (currentquestion)) + "%"); }); } }; }); }); 
 .ui-widget-header { background-image: none !important; background-color: #FF7860 !important; //Any colour can go here } label { display: block; text-align: center; } /*input[name="option"] { float:left; }*/ #progressbar { margin: auto; margin-top: 20px; float: none; width: 50%; /*height: 100%;*/ } #container { text-align: center; } span { margin: 5em; padding: 3em; } 
 <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <h3 id="question"></h3> <form id="form"> <input type="radio" name="option" value="0" id="option0" checked> <label for='option0'> <br/> </label> <input type="radio" name="option" value="1" id="option1"> <label for='option1'> <br/> </label> <input type="radio" name="option" value="2" id="option2"> <label for='option2'> <br/> </label> </form> <br/> <a href="#" id="next" class="button">Next</a> <br/> <div id="progressbar"></div> </div> 

更改您的display:block label中的行inline-block並刪除HTML中<label>之外的<br /> ,用div包裝label/input

 var allQuestions = [{ question: "Who is the Prime Minister of the United Kingdom?", choices: ["David Cameron", "Gordon Brown", "Winston Churchill"], correctAnswer: 0 }, { question: "Who is the Prime Minister of India?", choices: ["Rahul Gandhi", "Arvind Kejrival", "Narendra Damodardas Modi"], correctAnswer: 2 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump", "Barack Obama", "Hilary Clinton"], correctAnswer: 1 }, { question: " Who averaged one patent for every three weeks of his life?", choices: ["Thomas Edison", "Tesla", "Einstein"], correctAnswer: 0 }, { question: "What continent is cut into two fairly equal halves by the Tropic of Capricorn?", choices: ["Africa", "South America", "Australia"], correctAnswer: 2 }, { question: "What explorer introduced pigs to North America?", choices: ["Christopher Columbus", "Galileo", "Mussorie"], correctAnswer: 0 }, { question: "What F-word is defined in physics as a “nuclear reaction in which nuclei combine to form more massive nuclei”? ", choices: ["Furnace", "Fusion", "Fission"], correctAnswer: 1 }, { question: "What was the first planet to be discovered using the telescope, in 1781?", choices: ["Uranus", "Jupiter", "Mars"], correctAnswer: 0 }, { question: "Who is the chief minister of West Bengal, India?", choices: ["Buddhadev Bhattacharya", "Jyoti Basu", "Mamta Banerjee"], correctAnswer: 2 }, { question: "Which is the fastest growing religion in the world?", choices: ["Islam", "Christianity", "Hinduism"], correctAnswer: 0 } /* { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 }, { question: "Who is the Prime Minister of America?", choices: ["Donald Trump","Barack Obama","Hilary Clinton"], correctAnswer:1 } */ ]; $(document).ready(function() { var currentquestion = 0; var correctAnswers = 0; $(function() { $("#progressbar").progressbar({ value: 0 }); }); $('#question').html(allQuestions[currentquestion].question); $('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>"); $('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>"); $('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>"); $("#next").click(function() { if ($("input[name=option]:checked").val() == allQuestions[currentquestion].correctAnswer) { correctAnswers++; }; currentquestion++; $(function() { $("#progressbar").progressbar({ value: currentquestion * (allQuestions.length), }); }); if (currentquestion < allQuestions.length) { $('#question').html(allQuestions[currentquestion].question); $('label[for=option0]').html(allQuestions[currentquestion].choices[0] + "<br/>"); $('label[for=option1]').html(allQuestions[currentquestion].choices[1] + "<br/>"); $('label[for=option2]').html(allQuestions[currentquestion].choices[2] + "<br/>"); if (currentquestion == allQuestions.length - 1) { $('#next').html("Submit"); //$('#next').off("click"); $('#next').click(function() { // if($("input[name=option]:checked").val()==allQuestions[currentquestion].correctAnswer){ // correctAnswers++; // }; // alert(correctAnswers); alert("Your Score is " + correctAnswers + " out of " + currentquestion + " and your percentage is " + (correctAnswers * 100 / (currentquestion)) + "%"); }); } }; }); }); 
 .ui-widget-header { background-image: none !important; background-color: #FF7860 !important; } h3, #next { text-align: center; display:block } #form div { margin: auto; max-width: 205px } label { display: inline-block; } input[name^="option"] { float: left; } #progressbar { margin: auto; margin-top: 20px; float: none; width: 50%; /*height: 100%;*/ } 
 <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <link href="https://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css" rel="stylesheet" /> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="container"> <h3 id="question"></h3> <form id="form"> <div> <input type="radio" name="option" value="0" id="option0" checked> <label for='option0'></label> </div> <div> <input type="radio" name="option" value="1" id="option1"> <label for='option1'></label> </div> <div> <input type="radio" name="option" value="2" id="option2"> <label for='option2'></label> </div> </form> <br/> <a href="#" id="next" class="button">Next</a> <br/> <div id="progressbar"></div> </div> 

只需將CSS的標簽設置為顯示“行內阻止”即可:

的CSS

    label{
      display: inline-block;
      text-align: center;
    }

的HTML

    <form id="form">
          <label for='option0'></label><input type="radio" name="option" value="0" id="option0" checked><br />
          <label for='option1'></label><input type="radio" name="option" value="1" id="option1"><br />
          <label for='option2'></label><input type="radio" name="option" value="2" id="option2"><br />          

編輯

因此,兩個答案都不能解決問題...

您要做的是:

1)為您的標簽設置CSS:

label{   display: inline-block; }

2)為單選按鈕創建一個新類:

 .rad_in {
   position: absolute;
   left : 25%;
 }

3)然后您的html如下所示(帶有單選按鈕類):

  <form id="form"> <input class="rad_in" type="radio" name="option" value="0" id="option0" checked><label for='option0'></label><br /> <input class="rad_in" type="radio" name="option" value="1" id="option1"><label for='option1'></label><br /> <input class="rad_in" type="radio" name="option" value="2" id="option2"><label for='option2'></label><br /> </form> 

這是小提琴: https : //jsfiddle.net/8fv3cnc6/

看到我編輯的答案和小提琴:

.rad_in {
  position: absolute;
  left : 25%;
}

https://jsfiddle.net/8fv3cnc6/

PS:對不起,由於我無法發表評論,我不得不發表另一個答案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM