1

我有以下 html .. 1 个主 div 有 2 个子 div。我需要遍历两个 div 并在每个中检索选中的单选按钮。例如:第一个子 div 可能已检查,而第二个 div 已检查 wright,我需要检索它们的值。我该怎么做

<DIV>
 <DIV>
  <TABLE>
   <TBODY>
      <TR>
          <TD>
            wrong<INPUT class=XX disabled value=wrong type=radio name=radiobutton>
            wright<INPUT class=XX disabled value=wright type=radio name=radiobutton>
            maybe<INPUT class=XX value=maybe CHECKED type=radio name=radiobutton ></TD>
    </TR></TBODY></TABLE>
</DIV>
<DIV>
   <TABLE><TBODY><TR>
     <TD>
      wrong<INPUT class=XX CHECKED value=wrong type=radio name=radiobutton>
       wright<INPUT class=XX value=wright type=radio name=radiobutton>
       maybe<INPUT class=XX value=maybe type=radio name=radiobutton ></TD>
       </TR></TBODY></TABLE>
     </DIV>
</DIV>
4

2 回答 2

1

据我了解,您有两组单选按钮。

所以,这是我的解决方案:

HTML

<div>
  <div>
      <table>
          <tbody>
              <tr>
                  <td>wrong
                      <input class="xx" disabled="disabled" value="wrong" type="radio"
                      name="radiobutton">wright
                      <input class="xx" disabled="disabled" value="wright" type="radio"
                      name="radiobutton">maybe
                      <input class="xx" value="maybe" checked="checked" type="radio" name="radiobutton">
                  </td>
              </tr>
          </tbody>
      </table>
  </div>
  <div>
      <table>
          <tbody>
              <tr>
                  <td>wrong
                      <input class="xx" checked="checked" value="wrong" type="radio" name="radiobutton2">wright
                      <input class="xx" value="wright" type="radio" name="radiobutton2">maybe
                      <input class="xx" value="maybe" type="radio" name="radiobutton2">
                  </td>
              </tr>
          </tbody>
      </table>
  </div>
</div>​

jQuery

var radiobutton = $("input[name='radiobutton']:checked").val();
var radiobutton2 = $("input[name='radiobutton2']:checked").val();

alert('1st - '+radiobutton+' / 2nd - '+radiobutton2);​

演示

编辑

jQuery

var checked = new Array();
var pos = 0;
$("input[type='radio']:checked").each(function(){
    checked[pos] = new Array(2);
    checked[pos]['name'] = $(this).attr('name');
    checked[pos]['value'] = $(this).val();
    pos++;
});

var result = "";
for(i=0; i<pos; i++){
    result += checked[i]['name'] + " - " + checked[i]['value'] + " / ";
}
alert(result);

演示

于 2012-07-26T00:27:48.780 回答
0

这是你在找什么:工作演示 http://jsfiddle.net/79syA/

在您的 HTML 中,您只wrong检查了输入单选,也将您的 html 设为小写,是否有任何理由将其设为大写:)

休息一下,希望这符合您的需求,并随时使用演示!:)

代码

$('.XX').each(function(){

    if($(this).is(':checked')){
       alert(" Checked value is ==> " + this.value);
    }        

})​
于 2012-07-26T00:24:46.920 回答