问题描述
我有问题,我需要将输入类型复选框与输入类型文本关联.
I have a problem, I need to associate a input type checkbox to a input type text.
情况如下:
从数据库中提取数据. PK数据是复选框的值.复选框选择输入类型的文本时,您可以在其中输入特定数字.
Extract data from a database. The PK data are the value of the checkbox. When a checbox select an input type text where you can enter a specific number is activated.
现在的问题是,选择复选框输入文本后,所有类型的文本都被激活.我希望通过选择复选框输入,仅启用与复选框相关联的输入.
Now the problem is that selecting a checkbox input text all kinds are activated. And what I hope is that by selecting the checkbox input only the input associated with the checkbox enabled.
我的HTML代码(此代码创建一个输入复选框并为数据库中的每个记录输入文本,而我要激活的是激活特定复选框,即输入类型文本):
My HTML code (This code creates a input checkbox and input text for each record in the database, and what I want is to activate a specific checkbox is activated, the input type text):
<form action="send.php" method="POST" id="form-touch">
<?php foreach ($data as $key): ?>
<div id="inputsForm">
<input type="checkbox" class="id-check" name="nuevoid[]" value="<?php echo $key['pr_id'] ?>">
<input type="newquantity[]" class="myinput" disabled name="newQuantity" value="" placeholder="Enter quantity">
</div>
<?php endforeach; ?>
<input type="submit" value="Send">
</form>
我的jquery代码(通过选中一个复选框来激活或停用文本字段的代码):
My jquery code (code that activates or deactivates the text field by selecting a checkbox):
$('.id-check').change(function(){
if ($('.id-check').is(':checked') == false){
$('.myinput').val('').prop('disabled', true);
} else {
$('.myinput').val('1').prop('disabled', false);
}
});
我如何实现自己想要的?智利的问候.
How I can achieve what I want? Greetings from Chile.
推荐答案
尝试
$('.id-check').change(function() {
if ($(this).is(':checked') == false) {
$(this).closest('div').find('.myinput').val('').prop('disabled', true);
} else {
$(this).closest('div').find('.myinput').val('1').prop('disabled', false);
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form action="send.php" method="POST" id="form-touch">
<div id="inputsForm">
<input type="checkbox" class="id-check" name="nuevoid[]" value="">
<input type="newquantity[]" class="myinput" disabled name="newQuantity" value="" placeholder="Enter quantity">
</div>
<div id="inputsForm">
<input type="checkbox" class="id-check" name="nuevoid[]" value="1">
<input type="newquantity[]" class="myinput" disabled name="newQuantity" value="" placeholder="Enter quantity">
</div>
<input type="submit" value="Send">
</form>
这篇关于将输入类型复选框关联到输入类型文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!