[英]Get automatic selected checkbox and next input text value in php
I want to get automatic the checkbox value and the next input text value when checked. 我想在选中时自动获得复选框值和下一个输入文本值。 Please don't tell me 'give name like name1, name2 etc' because I want to get data from my database.
请不要告诉我“给定名称,例如name1,name2等”,因为我想从数据库中获取数据。
<?php
if(isset($_GET['submit'])){
$amount = $_GET['amount'];
$checked= $_GET['check'];
echo "You selected ".$amount.". of ".$checked."";
}
?>
<form>
<input type="checkbox" value="Coke" name="check[]">Coke<input type="number" name="amount[]"><br>
<input type="checkbox" value="Fanta" name="check[]">Fanta<input type="number" name="amount[]"><br>
<input type="checkbox" value="Water" name="check[]">Water<input type="text" name="amount[]"><br>
<input type="submit" value='Verder' name="submit">
</form>
You can do this using jquery: 您可以使用jquery执行此操作:
$("input[type=checkbox]" ).on( "click", function(){
if($(this).is(':checked')){
var value_of_checkbox = $(this).val();
var value_of_next_input = $(this).next().val();
//DO more stuff
}
});
Hope this will helps you.You can achieve this by using jquery. 希望对您有所帮助。您可以使用jquery来实现。
$(function(){
$("input[name='check[]']").click(function(){
var isChecked = $(this).is(":checked");
if (isChecked) {
var nextInputvalue = $(this).next(':input[name="amount[]"]').val();
alert(nextInputvalue); // this will give the text box value
}
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.