无法将单选按钮与文本对齐

时间:2016-03-12 16:07:34

标签: jquery html css html5 css3

我打算将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>

3 个答案:

答案 0 :(得分:1)

dt[, Grp := rleid(x1,x2)] 中的display:block更改为label并删除HTML中inline-block之外的<br />,并使用{<label>label/input {1}}

div
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%;*/
}

答案 1 :(得分:0)

只需设置标签的CSS即可显示&#34;内联块&#34;:

<强> 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/

答案 2 :(得分:0)

查看我编辑的答案和小提琴:

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

https://jsfiddle.net/8fv3cnc6/

PS:对不起,我不得不发表另一个答案,因为我无法发表评论......