小编Joh*_*ohn的帖子

使用jQuery Validate在条件上应用验证

早上好,我想知道您是否可以在这个问题上对我有所帮助:当另一个输入值满足条件时,我将根据需要对select元素应用验证。我正在使用验证jQuery插件来做到这一点。这是伪代码:(if(textbox == "valueX"){mySelectEelement is required;}表示我必须从select元素中选择一个值。)。因此出于某种原因,select元素未采用我想应用的验证。

请查看我为此目的在Plunker上创建的完整示例代码:

<html lang="en">
<head>
 <title></title>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.9.1.js"></script>
<script src="https://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>
<script>
$( function() {
$.validator.setDefaults({
      //debug: true,
      success: "valid"
    });

    $( "#myform" ).validate({
      rules: {
        borough: {
              required: false
        }
      }
    });

    $('#state').on('change', function () {
    if ($(this).val().toUpperCase() == "NY" || $(this).val().toUpperCase() == "NEW YORK") {
        $('#borough').rules('add', {
            required: true
        });
    } else{
       $('#borough').rules('remove');
    }
});    
} );
</script>

</head>
<body>
<form id="myform">
<div>
<p id="pid"></p>
<input id='text' type='text' …
Run Code Online (Sandbox Code Playgroud)

jquery jquery-validate

-1
推荐指数
1
解决办法
1450
查看次数

标签 统计

jquery ×1

jquery-validate ×1