51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

使用jQuery根据复选框验证文本框。

英文:

Jquery - validate textbox based on checkbox

问题 {#heading}

I want to validate a value only when a checkbox is checked. I am able to get the validation running but if the checkbox is unchecked, the validation still runs.

Here is my code:

$("#personal_deliver_same").on("click", function(){
  var check = $("#personal_deliver_same").is(':checked');
  if(check){
    $("#input-payment-postcode").keyup(function (){ 
      if($("#input-payment-postcode").val().length > 5) {
        alert("in2");
      }
    });		
  }
  else{
    alert("out2");
  }
});		

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<label class="control-label" for="input-payment-postcode">Post Code</label>
<input type="text" name="personal_details[postcode]" value="" placeholder="" id="input-payment-postcode" class="formcontrol">
<br>
<input type="checkbox" id="personal_deliver_same" name="personal_details[shipping_address]" value="1" />
<label>My Checkbox</label>

英文:

I want to validate a value only when a checkbox is checked. I am able to get the validation running but if the checkbox is unchecked, the validation still runs.

Here is my code:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&quot;#personal_deliver_same&quot;).on(&quot;click&quot;, function(){
  var check = $(&quot;#personal_deliver_same&quot;).is(&#39;:checked&#39;);
  if(check){
    $(&quot;#input-payment-postcode&quot;).keyup(function (){ 
      if($(&quot;#input-payment-postcode&quot;).val().length &gt; 5) {
        alert(&quot;in2&quot;);
      }
    });		
  }
  else{
    alert(&quot;out2&quot;);
  }
});		

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
`&lt;label class=&quot;control-label&quot; for=&quot;input-payment-postcode&quot;&gt;Post Code&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;personal_details[postcode]&quot; value=&quot;&quot; placeholder=&quot;&quot; id=&quot;input-payment-postcode&quot; class=&quot;formcontrol&quot;&gt;
&lt;br&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;personal_deliver_same&quot; name=&quot;personal_details[shipping_address]&quot; value=&quot;1&quot; /&gt;
&lt;label&gt;My Checkbox&lt;/label&gt;
`

<!-- end snippet -->

答案1 {#1}

得分: 1

I think this will be what you want.

  • 它将把复选框的值存储在一个变量中

  • 它将监视复选框的 input 事件,而不仅仅是 click。现在当值发生变化时,事件将发生

  • 它将监视文本框的 input 事件以检测变化。如果发生变化并且复选框被选中,它将显示输出

  • 当复选框被选中时,它也会触发文本框上的事件

    const $checkbox = $("#personal_deliver_same"); const $txtPostCode = $("#input-payment-postcode"); let isChecked = $checkbox.is(":checked");

    $checkbox.on("input", function(){ isChecked = $checkbox.is(":checked"); console.log("Checkbox changed:", isChecked);

    if(isChecked){
      $txtPostCode.trigger('input');
    }
    

    });

    $txtPostCode.on('input', function (){ if(isChecked) { if($txtPostCode.val().length > 5){ console.log("post code length is more than 5"); } else { console.log("post code length is 5 or less"); } } });


请注意,这些是您提供的代码的中文翻译部分,不包括问题或其他内容。 英文:

I think this will be what you want.

  • It will store the value of the checkbox in a variable
  • it will monitor the checkbox's input event instead of just click. Now when the value changes the event will occur
  • It will monitor the input event of the text box for changes. If a change happens and the box is checked it will show you the output
  • When the box is checked, it will also fire the event on the textbox

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

const $checkbox = $(&quot;#personal_deliver_same&quot;);
const $txtPostCode = $(&quot;#input-payment-postcode&quot;);
let isChecked = $checkbox.is(&quot;:checked&quot;);

$checkbox.on(\&quot;input\&quot;, function(){
isChecked = $checkbox.is(\&quot;:checked\&quot;);
console.log(\&quot;Checkbox changed:\&quot;, isChecked);


if(isChecked){
$txtPostCode.trigger(\&#39;input\&#39;);
}
});

`$txtPostCode.on(&#39;input&#39;, function (){
if(isChecked) {
if($txtPostCode.val().length &gt; 5){
console.log(&quot;post code length is more than 5&quot;);
} else {
console.log(&quot;post code length is 5 or less&quot;);
}
}
});
`

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
`&lt;label class=&quot;control-label&quot; for=&quot;input-payment-postcode&quot;&gt;Post Code&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;personal_details[postcode]&quot; value=&quot;&quot; placeholder=&quot;&quot; id=&quot;input-payment-postcode&quot; class=&quot;formcontrol&quot;&gt;
&lt;br&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;personal_deliver_same&quot; name=&quot;personal_details[shipping_address]&quot; value=&quot;1&quot; /&gt;
&lt;label for=&quot;personal_deliver_same&quot;&gt;My Checkbox&lt;/label&gt;
`

<!-- end snippet -->

答案2 {#2}

得分: 1

你正在错误的顺序绑定你的事件处理程序,并且在需要时没有进行检查。尝试这种方式:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label class="control-label" for="input-payment-postcode">邮编</label>
<input type="text" name="personal_details[postcode]" value="" placeholder="" id="input-payment-postcode" class="formcontrol">
<br>
<input type="checkbox" id="personal_deliver_same" name="personal_details[shipping_address]" value="1">
<label>我的复选框</label>

var check = false;

$("#personal_deliver_same").on("click", function() {
  check = $("#personal_deliver_same").is(':checked');
});

$("#input-payment-postcode").keyup(function() {
  if (check && $("#input-payment-postcode").val().length > 5) {
    console.log("in2");
  }
});

(注意:我已经将代码和HTML分别翻译,没有翻译的部分不包括在内。) 英文:

You're binding your event handlers in the wrong sequence and not checking it when you need to. Try this way:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

var check = false;

$(\&quot;#personal_deliver_same\&quot;).on(\&quot;click\&quot;, function() {
check = $(\&quot;#personal_deliver_same\&quot;).is(\&#39;:checked\&#39;);
});

`$(&quot;#input-payment-postcode&quot;).keyup(function() {
if (check &amp;&amp; $(&quot;#input-payment-postcode&quot;).val().length &gt; 5) {
console.log(&quot;in2&quot;);
}
});
`

<!-- language: lang-html -->

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
`&lt;label class=&quot;control-label&quot; for=&quot;input-payment-postcode&quot;&gt;Post Code&lt;/label&gt;
&lt;input type=&quot;text&quot; name=&quot;personal_details[postcode]&quot; value=&quot;&quot; placeholder=&quot;&quot; id=&quot;input-payment-postcode&quot; class=&quot;formcontrol&quot;&gt;
&lt;br&gt;
&lt;input type=&quot;checkbox&quot; id=&quot;personal_deliver_same&quot; name=&quot;personal_details[shipping_address]&quot; value=&quot;1&quot; /&gt;
&lt;label&gt;My Checkbox&lt;/label&gt;
`

<!-- end snippet -->

答案3 {#3}

得分: 0

<label for="postcode">邮政编码</label>
<input id="postcode" />

<input id="validate" type="checkbox">
<label for="validate">验证邮政编码?</label>

<script>
  $('#validate').on('click', () => shouldValidate())
  $('#postcode').on('keyup', () => shouldValidate())

  function shouldValidate(){
    if( $('#validate').is(':checked') )
      // 验证
    else
      // 不验证
  }
</script>

英文:

&lt;label for=&quot;postcode&quot;&gt;Post Code&lt;/label&gt;
&lt;input id=&quot;postcode&quot; /&gt;

\&lt;input id=\&quot;validate\&quot; type=\&quot;checkbox\&quot;\&gt;
\&lt;label for=\&quot;validate\&quot;\&gt;Validate Post Code?\&lt;/label\&gt;


\&lt;script\&gt;
$(\&#39;#validate\&#39;).on(\&#39;click\&#39;,() =\&gt; shouldValidate() )
$(\&#39;#postcode\&#39;).on(\&#39;keyup\&#39;,() =\&gt; shouldValidate() )

`function shouldValidate(){
if( $(&#39;#validate&#39;).is(&#39;:checked&#39;) )
// Validate
else
// Don&#39;t Validate
}
&lt;/script&gt;
`


赞(1)
未经允许不得转载:工具盒子 » 使用jQuery根据复选框验证文本框。