从每个组中选择至少一个单选按钮(使用jquery)

gio*_*iom 13 jquery radio-button

我正在动态拉动并在表单上显示单选按钮问题(由jquery选项卡分隔成不同的部分).

所以有很多2,3,4,5等单选按钮,它们具有相同的"名称"但不同的ID.

当您点击两个部分之间的下一个时,我想检查是否为每个组选择了至少一个单选按钮.

(当表单加载时,默认情况下单选按钮没有选中,必须这样.)

我可以遍历当前部分的单选按钮,但我不确定如何轻松检查是否已为每个组检查了一个?

Dav*_*mas 30

在元素之间没有任何关系,或者如何找到组名,我只能给你一个指针,但这应该有用(通过点击元素触发检查id="nextButton",但显然这可以定制为任何其他适当的事件,$('form').submit()例如):

$(document).ready(
function(){
  $('#nextButton').click(
    function(){

    var radioName = something; // use this if there's a relationship between the element
                               // triggering this check and the radio buttons

      if ($('input[name='+ radioName +']:checked').length) {
           // at least one of the radio buttons was checked
           return true; // allow whatever action would normally happen to continue
      }
      else {
           // no radio button was checked
           return false; // stop whatever action would normally happen
      }
    }
  );
}
);
Run Code Online (Sandbox Code Playgroud)


eKe*_*ek0 5

$("input[@name='the_name']:checked").length;
Run Code Online (Sandbox Code Playgroud)

  • 自jQuery 1.1.4起(在评论部分中,不推荐使用@语法)(http://api.jquery.com/attribute-equals-selector/)。 (3认同)