使用jQuery在标签内移动输入

时间:2016-10-11 16:30:48

标签: javascript jquery html

如何在<label>标记内移动输入,以便使用jQuery <label><input></label>。我需要定位select-option集合中的所有答案。

&#13;
&#13;
<div class=”select-option”>
<div>
 <input id="[0]_Actual_Answer_1" name="[0]_Actual_Answer_1" type="radio" value="IZWE">
 <label for="[0]_Actual_Answer_1">HELLO WORLD</label>
</div>
<div>
 <input id="[0]_Actual_Answer_2" name="[0]_Actual_Answer_2" type="radio" value="IZWE">
 <label for="[0]_Actual_Answer_2">HELLO WORLD 2</label>
</div>
<div>
 <input id="[0]_Actual_Answer_3" name="[0]_Actual_Answer_3" type="radio" value="IZWE">
 <label for="[0]_Actual_Answer_3">HELLO WORLD 3</label>
</div>
<div>
 <input id="[0]_Actual_Answer_4" name="[0]_Actual_Answer_4" type="radio" value="IZWE">
 <label for="[0]_Actual_Answer_4">HELLO WORLD 4</label>
</div>
</div>
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:0)

如果我理解正确,你可能会追求的是......

$(document).ready(function() {
  $('.select-option input').each(function() { //Get every input in the "select-option" div
     var t = $(this);
     var l = t.siblings('label'); //Find the closest label
     t.detach().appendTo(l);
  });
});

JSFiddle

答案 1 :(得分:0)

您的报价<div class=”select-option”>不正确!使用"
比jQuery部分:

&#13;
&#13;
$("label[for*='[0]_Actual_Answer']").prepend(function(){
  return $(this).prev("input");
});
&#13;
label{background:red;}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="select-option">
  <div>
    <input id="[0]_Actual_Answer_1" name="[0]_Actual_Answer_1" type="radio" value="IZWE">
    <label for="[0]_Actual_Answer_1">HELLO WORLD</label>
  </div>
  <div>
    <input id="[0]_Actual_Answer_2" name="[0]_Actual_Answer_2" type="radio" value="IZWE">
    <label for="[0]_Actual_Answer_2">HELLO WORLD 2</label>
  </div>
  <div>
    <input id="[0]_Actual_Answer_3" name="[0]_Actual_Answer_3" type="radio" value="IZWE">
    <label for="[0]_Actual_Answer_3">HELLO WORLD 3</label>
  </div>
  <div>
    <input id="[0]_Actual_Answer_4" name="[0]_Actual_Answer_4" type="radio" value="IZWE">
    <label for="[0]_Actual_Answer_4">HELLO WORLD 4</label>
  </div>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:0)

使用For Each mchkb In MyCheckBoxesCollection

prepend(function)