我在php中有一个查询,该查询从表中收集产品并按它们,我希望通过单击复选框使输入可见,直到现在我都这样:
<?php
$conn->conectar();
$query = "SELECT * FROM c_ingredientes WHERE precio = '5'";
try {
$resp = $conn->obtDatos($query);
if ($conn->filasConsultadas > 0) {
foreach ($resp as $dts) {
$id = $dts['id'];
$ingrediente = $dts['ingrediente'];
echo "<style>
#quiantitynice{
display: none;
}
</style>
<div name=\"quiantitynice$id\">
<input type=\"number\" name=\"quantity$id\">
</div>";
echo "<div class=\"form-check\">
<input class=\"form-check-input\" name=\"extra\" type=\"checkbox\" id=\"extra$id\" value=\"$id\">
<label class=\"form-check-label\" for=\"extra$id\">" . utf8_encode($ingrediente) . "</label>
</div>";
}
}
}
catch (Exception $ex) {
echo $ex;
}
$conn->cerrar();
?>
这是我在脚本中拥有的,但是我不知道如何连接它,因为每个复选框都有其名称+ PHP ID,并且它们已经生成:
<script>
$(document).ready(function() {
$('#extra').on('change', function() {
if (this.checked) {
$("#quantity$id").show();
} else {
$("#quantity&id").hide();
}
})
});
</script>
答案 0 :(得分:1)
您可以通过以下步骤来实现;
$(document).ready(function(){
$('[name="extra"]').on('change',function () {
var _thisVal = $(this).val();
if (this.checked) {
$("#quiantitynice" + _thisVal).show();
} else {
$("#quiantitynice" + _thisVal).hide();
}
});
});
.quiantitynice {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-check">
<input class="form-check-input" name="extra" type="checkbox" id="extra1" value="1">
<label class="form-check-label" for="extra1">ingredientname1</label>
</div>
<div class="quiantitynice" id="quiantitynice1">
<input type="number" name="quantity1">
</div>
<div class="form-check">
<input class="form-check-input" name="extra" type="checkbox" id="extra$id" value="2">
<label class="form-check-label" for="extra1">ingredientname2</label>
</div>
<div class="quiantitynice" id="quiantitynice2">
<input type="number" name="quantity2">
</div>