[英]How I can uncheck checkbox when both of my inputs [type=text] cleared or empty?
我怎樣才能取消input
時,我的兩個類型復選框的input
S型文本是空的,如果我的一個檢查input
類型的文本 s的填充?
$('input[name="t2"],input[name="t3"]').keyup(function() { $('input[name="t1"]').prop("checked", $.trim($(this).val()).length != 0); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" name="t1" /> Price range from: <input type="text" name="t2" /> to <input type="text" name="t2" />
JSFiddle: 在此鏈接中
當我在任何輸入類型的文本上鍵入內容時,它會正確執行(選中了復選框),但是當我清除兩個字段時都無法正常工作,因為當僅清除一個輸入時,復選框未選中。 您能幫我解決這個問題嗎? 謝謝
您觸發了兩次。 您只需要檢查兩個字段中是否有輸入。 像這樣:
$('input[name="t2"],input[name="t3"]').keyup(function () {
$('input[name="t1"]').prop("checked",
//now check if any of the two inputs have content
$.trim($('input[name="t2"]').val()).length != 0 ||
$.trim($('input[name="t3"]').val()).length != 0
);
});
您可以簡單地通過這種方式來獲得每個輸入的值。 如果兩者均為空,則取消選中;如果其中一個已填充,則選中該框。
$('input[name="t2"],input[name="t3"]').keyup(function() { var t2 = $('input[name="t2"]').val(); var t3 = $('input[name="t3"]').val(); if (t2 != "" || t3 != "") { $('input[name="t1"]').prop("checked", true); } else { $('input[name="t1"]').prop("checked", false); } });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js"></script> <input type='checkbox' name='t1'> <input type="text" name="t2"> <input type="text" name="t3">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.