2

用户必须从三个输入“类别”中的每一个中选择一个收音机。如果他“提交”而不这样做,他会收到警告:

http://jsfiddle.net/bqyvS/

像这样的标记:

<form>
    <div id="color">
        <input type="radio" name="color" id="blue">
        <label for="blue">Blue</label>
        <input type="radio" name="color" id="red">
        <label for="red">Red</label>
        <input type="radio" name="color" id="green">      
        <label for="green">Green</label>
    </div>
    <div id="shape">
        <input type="radio" name="shape" id="square">
        <label for="square">Square</label>
        <input type="radio" name="shape" id="circle">
        <label for="circle">Circle</label>
        <input type="radio" name="shape" id="triangle">
        <label for="triangle">Triangle</label>
    </div>
    <div id="size">
        <input type="radio" name="size" id="small">
        <label for="small">Small</label>
        <input type="radio" name="size" id="medium">
        <label for="mediume">Medium</label>
        <input type="radio" name="size" id="large">      
        <label for="large">Large</label>
    </div>
  </form>
<a id="link" href="#">click me to "submit"</a>
<p id="warning"></p>​

Javascript:

$('#link').on('click', function() {
   if (!$('#color input[type=radio]:checked').length) {
      $('#warning').html("Oops! Please choose a color!"); 
   }
   else if(!$('#shape input[type=radio]:checked').length) {
      $('#warning').text("Oops! Please choose a shape!"); 
   }
   else if(!$('#size input[type=radio]:checked').length) {
     $('#warning').text("Oops! Please choose a size!"); 
   } 
});

这是一段较大代码的简化版本。我怎样才能更有效地重写条件,这样我就不会详细检查每个输入名称?(即使没有检查多个输入名称类别,每次“提交”也应该只显示一个“警告”。)编辑标记是可以的。

谢谢!

4

3 回答 3

2

将行为应用于类似的组时,您应该开始考虑类而不是 id,在此解决方案中,您不需要单独的数据名称,但我相信将数据与 html id 分开会更好,但您可以使用它。 id 如果你愿意

<form>
    <div id="color" class="selection-group" data-name="color">
        <input type="radio" name="color" id="blue">
        <label for="blue">Blue</label>
        <input type="radio" name="color" id="red">
        <label for="red">Red</label>
        <input type="radio" name="color" id="green">      
        <label for="green">Green</label>
    </div>
    <div id="shape" class="selection-group" data-name="square">
        <input type="radio" name="shape" id="square">
        <label for="square">Square</label>
        <input type="radio" name="shape" id="circle">
        <label for="circle">Circle</label>
        <input type="radio" name="shape" id="triangle">
        <label for="triangle">Triangle</label>
    </div>
    <div id="size" class="selection-group" data-name="size">
        <input type="radio" name="size" id="small">
        <label for="small">Small</label>
        <input type="radio" name="size" id="medium">
        <label for="mediume">Medium</label>
        <input type="radio" name="size" id="large">      
        <label for="large">Large</label>
    </div>
  </form>
<a id="link" href="#">click me to "submit"</a>
<p id="warning"></p>​

Javascript:

$('#link').on('click', function() {
   $('.selection-group').each(function() {
      if(!$(this).find('input[type=radio]:checked').length) {
          $('#warning').html("Oops! Please choose a "+ $(this).data('name') +"!"); 
          return false;
      }
   });
});
于 2012-08-21T19:15:20.733 回答
2
function validationCheck() {
    var isValid = true,
        errorText = "";
    $("form div").each(  //get the divs that hold the radios and loop
    //$("#color, #shape, #size").each( //could do it by ids of the divs also
        function(){
            var div = jQuery(this),  //div reference
                isChecked = div.find('input[type="radio"]:checked').length>0;  //see if we have anything selected
            if (!isChecked) {  //if no selections, show error message
                isValid = false;  //set validation to false
                errorText = "Oops! Please choose a " + div.prop("id") + "!";  //build error message
                return false;  //exit each loop
            }
        }
    );
    $('#warning').text(errorText); //set error message
    return isValid;
}
于 2012-08-21T19:10:01.717 回答
1
$('#link').on('click', function(){
  $('#color, #shape, #size').each(function(i, ele){
      if($(this).find('input:checked').length == 0)
        $('#warning').text("Oops! Please choose a " + this.id + "!");
  });
}); 

jsFiddle

您可能需要考虑在用户未选择任何输入或仅选择 1 个输入的情况下生成警告消息。

在此示例中,用户将收到类似于以下内容的消息Oops! Please choose a color, and shape!

$('#link').on('click', function(){
  var msgs = [];

  $('#color, #shape, #size').each(function(i, ele){
    if($(this).find('input:checked').length == 0)
      msgs.push(this.id);
  });

  if(msgs.length > 0)
    $('#warning').text("Oops! Please choose a " + msgs.join(", and "));
}); 

jsFiddle

于 2012-08-21T19:10:25.640 回答