0

我有一个动态的文本框列表来捕获一个或多个货币金额。我希望至少一个文本框具有有效的货币金额。

我无法让它完全正常工作。我真的很感激任何想法或建议。

这是我到目前为止...

<html>
  <head>
  <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.js"></script>
  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/themes/cupertino/jquery-ui.css" rel="stylesheet" type="text/css" />
  <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js"></script>
  <script type="text/ecmascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/jquery.validate.min.js"></script>
  <script type="text/ecmascript" src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.10.0/additional-methods.min.js"></script>
  <script type="text/javascript" src="js/jquery.maskedinput-1.3.min.js"></script> 

  <div id="page_wrap"><form action="http://local.host.com/myform/net/send" method="post" accept-charset="utf-8" id="myform">
  <h2>Test</h2>
  <h3>Enter an amount and click send</h3>
  <?php //foreach ($recs as $rec): ?>
  <Fieldset>
    <label for="Amount_1">Amount</label>
    <input name="Amount_1" type="text" id="Amount_1" />
  </Fieldset>
  <Fieldset>
    <label for="Amount_2">Amount</label>
    <input name="Amount_2" type="text" id="Amount_2" />
  </Fieldset>
  <Fieldset>
    <label for="Amount_3">Amount</label>
    <input name="Amount_3" type="text" id="Amount_3" />
  </Fieldset>
  <?php // endforeach; ?>

  <span class="button right"><input type="submit" value="Send" /></span>
  <br class="flt_clear" />
  </form>
    <script>
    $(document).ready(function() {
      $('input[name^="Amount_"]').mask("$?999");    
      $.validator.addMethod("pickAtLeastOne", function(value, element, param) {
        return $('input[name^="Amount_"]').val() == "$";
      });
      $('#myform').validate();
      $('input[name^="Amount_"]').each(function() {
      $(this).rules("add", {
        pickAtLeastOne: true,
        messages: {
          pickAtLeastOne: "Please enter at least one amount"
        }
      });
    });
    });
    </script>
  </body>
</html>
4

3 回答 3

1

您应该检查当前正在测试的元素,而不是整个组选择器,因此请尝试替换以下代码:

return $('input[name^="Amount_"]').val() == "$";

对于这个:

return value == "$";

因为在

于 2012-10-09T16:26:18.913 回答
1

试试这个,而不是我在 txtBox 上使用类数量,你可以修改它你想要的和工作代码FIDDLE

$.validator.addMethod("pickAtLeastOne", function(value, element, param) {
   var retCode = false;
   var txtBoxLength = $('.amount').length;
   var ctr = 0;
   $('.amount').each(function(value, element) {
     if ($(this).val() == ''){
       ctr++;
     }
  });
 return ctr == txtBoxLength;
});
于 2012-10-12T04:44:07.060 回答
0

这是我如何让它工作的。除了我的 pickAtLeastOne 方法之外,一切都是正确的。这是工作的:

$.validator.addMethod("pickAtLeastOne", function(value, element, param) {
  var retCode = false;
  $('input[name^="Amount_"]').each(function(value, element) {
    if ($(this).val() != '$' && $(this).val() != '') {
      retCode = true;
      return;
    }
  });
  return retCode;
});
于 2012-10-12T04:28:35.927 回答