从主div中的子div中检索radiobuttons

时间:2012-07-26 00:18:36

标签: javascript jquery html

我有以下html .. 1主div有2个子div。 我需要遍历两个div并检索每个div中的checked radiobutton。例如:第一个子div可能已经检查过,而第二个div已经检查过,我需要检索它们的值。我该怎么做呢

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

2 个答案:

答案 0 :(得分:1)

根据我的理解,你有两组radiobuttons。

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

  

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);​

DEMO

修改

  

的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);

DEMO

答案 1 :(得分:0)

这是您要找的内容:工作演示 http://jsfiddle.net/79syA/

在您的HTML中,您只选中了wrong输入广播,并使您的html小写有任何理由为什么它是大写的:)

休息希望这符合您的需求&amp;随意玩演示! :)

<强>码

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

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

})​