admin管理员组文章数量:1334152
I’m trying to make a form that looks like this:
where if inside of the input type ="text" is something like a number auto-check the CHECKBOX, is jquery? or simple PHP.
Ahh, the more important thing, I'm using woomerce, so i make the function like that.
add_action('woomerce_after_order_notes', 'my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
woomerce_form_field( 'losung', array(
'type' => 'text',
'class' => array('my-field-class'),
'label' => __('titel'),
'placeholder' => __('Enter a number'),
), $checkout->get_value( 'my_field_name' ));
echo '<div class="spiele-container">';
echo '<input class="input-checkbox pull-left" id="" type="checkbox" name="ter">';
echo '<div id="text-container-spiele">text ';
echo '</div>';
echo '</div>';
}
I’m trying to make a form that looks like this:
where if inside of the input type ="text" is something like a number auto-check the CHECKBOX, is jquery? or simple PHP.
Ahh, the more important thing, I'm using woomerce, so i make the function like that.
add_action('woomerce_after_order_notes', 'my_custom_checkout_field');
function my_custom_checkout_field( $checkout ) {
woomerce_form_field( 'losung', array(
'type' => 'text',
'class' => array('my-field-class'),
'label' => __('titel'),
'placeholder' => __('Enter a number'),
), $checkout->get_value( 'my_field_name' ));
echo '<div class="spiele-container">';
echo '<input class="input-checkbox pull-left" id="" type="checkbox" name="ter">';
echo '<div id="text-container-spiele">text ';
echo '</div>';
echo '</div>';
}
Share
Improve this question
edited Feb 3, 2014 at 15:04
Chris
137k133 gold badges305 silver badges283 bronze badges
asked Feb 3, 2014 at 14:44
DeimosDeimos
2691 gold badge7 silver badges19 bronze badges
2
- can you provide a fiddle or something? – user2897690 Commented Feb 3, 2014 at 14:52
- 1 Nothing to do with PHP in this case, it's a jquery/javascript thing. – Populus Commented Feb 3, 2014 at 14:56
3 Answers
Reset to default 5You can do something like this :
HTML :
<input type="text" class="something" />
<br>
<input type="checkbox" name="check" class="check">
JS:
$("input[type='text']").on("keyup", function(){
if(this.value!=""){
$("input[type='checkbox']").prop("checked", "checked");
}else{
$("input[type='checkbox']").prop('checked', "");
}
});
Demo : http://jsbin./anANoSof/1/
Cleaner solution would be to use a class/id for the text-field as well as the checkbox and use like :
HTML :
<input type="text" class="num" />
<br>
<input type="checkbox" name="check" class="check">
JS:
$(".num").on("keyup", function(e){
if(this.value!=""){
$(".check").prop("checked", "checked");
}else{
$(".check").prop('checked', "");
}
});
Demo : http://jsbin./iWESuTa/1/
As per @Fred, to disable the user from clicking the checkbox manually, simply use the disabled
attribute as follows :
<input type="checkbox" disabled name="check" class="check">
Demo : http://jsbin./iWESuTa/2/
Use this jQuery code.
consider you have id tbox
for textbox and id cbox
for checkbox.
$( "#tbox" ).keypress(function() {
$('#cbox').prop('checked', true);
});
keypress is the function that will trigger when a character is entered in the textbox.
the prop function is jQuery 1.6+.
well, this a JS task ( mostly everything going on without page reload is about JS). Using jQuery the code u need is something like
$(function(){
$('.my-field-class').keyup(function(){
var $cb = $(this).parents('form:eq(0)').find('.input-checkbox');
if(this.value != '')
$cb.attr('checked', 'checked');
else
$cb.removeAttr('checked');
});
});
本文标签: javascriptCheck checkbox if i write something in a input type textStack Overflow
版权声明:本文标题:javascript - Check checkbox if i write something in a input type text - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742340513a2456506.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论