获取mysql查询填充的webform中所有复选框的状态



我有一个由mysql对象填充的web表单。

这是我当前的网络表单:

if($statement_opc->execute())
{
$numero_opciones = 0;
$result_opc = $statement_opc->fetchAll();
foreach($result_opc as $row_opc)
{
if ($row_opc['producto'] == $row['id']) {
$numero_opciones = $numero_opciones + 1;
$output .= '
<div class="checkbox">
<label><input type="checkbox"  id="opcion'.$row["id"].'"  value="">'.$row_opc['nombre'].' (+ $'.$row_opc['precio'].')</label>
</div>
<input type="hidden" name="hidden_name" id="opcion_nombre'.$row["id"].'" value="'.$row_opc["nombre"].'" />
<input type="hidden" name="hidden_name" id="opcion_precio'.$row["id"].'" value="'.$row_opc["precio"].'" />
';
}
}
}
$cat_anterior = $row['id_cat'];
$output .= '
<input type="text" name="quantity" id="quantity' . $row["id"] .'" class="form-control" value="1" style="text-align:right;"/>
<br>
<input type="hidden" name="hidden_name" id="name'.$row["id"].'" value="'.$row["name"].'" />
<input type="hidden" name="hidden_price" id="price'.$row["id"].'" value="'.$row["price"].'" />
<div class="input-group">
<span class="input-group-bt style="text-align:LEFT">
<button type="button" class="btn btn-danger btn-number" name="restar"  id="'.$row["id"].'" " >
<span class="glyphicon glyphicon-minus"></span>
</button>
</span>
<span class="input-group-btn style="text-align:RIGHT">
<button type="button" class="btn btn-success btn-number-sumar" name="sumar" id="'.$row["id"].'"">
<span class="glyphicon glyphicon-plus"></span>
</button>
</span>
</div>
<input type="button" name="add_to_cart" id="'.$row["id"].'" style="margin-top:5px;" class="btn btn-success form-control add_to_cart" value="Add to Cart" />
</div>
</div>
</div>
';
}
//echo $titulo;
echo $output;

这是获取所有web表单对象的jQuery函数:

$(document).on('click', '.add_to_cart', function(){
var product_id = $(this).attr("id");
var product_name = $('#name'+product_id+'').val();
var product_price = $('#price'+product_id+'').val();
var product_quantity = $('#quantity'+product_id).val();
var product_opcion_nombre = $('#opcion_nombre'+product_id).val();
var product_opcion_precio = $('#opcion_precio'+product_id).val();
var action = "add";
if(product_quantity > 0)
{
$.ajax({
url:"action.php",
method:"POST",
data:{product_id:product_id, product_name:product_name, product_price:product_price, product_quantity:product_quantity,action:action},
success:function(data)
{
load_cart_data();
alert("Item has been Added into Cart");
}
});
}
else
{
alert("lease Enter Number of Quantity");
}
});

正如您可能看到的,我通过其product_id值获取所有objet,该值在使用附录的表单中声明$第['id']

每个web表单项可能有或没有其他对象,在这种情况下,新对象是由另一个MySQL查询填充的复选框。我指的是网络表单的这一部分:

$numero_opciones = 0;
$result_opc = $statement_opc->fetchAll();
foreach($result_opc as $row_opc)
{
if ($row_opc['producto'] == $row['id']) {
$numero_opciones = $numero_opciones + 1;
$output .= '
<div class="checkbox">
<label><input type="checkbox"  id="opcion'.$row["id"].'"  value="">'.$row_opc['nombre'].' (+ $'.$row_opc['precio'].')</label>
</div>
<input type="hidden" name="hidden_name" id="opcion_nombre'.$row["id"].'" value="'.$row_opc["nombre"].'" />
<input type="hidden" name="hidden_name" id="opcion_precio'.$row["id"].'" value="'.$row_opc["precio"].'" />
';
}
}
}

我能够从第一个foreach循环中获得所需的值,但我不知道如何声明复选框id,以获得选中复选框所需的数值,以便稍后在jQuery函数中使用。

这是我到目前为止所拥有的,但只有在第一个复选框被选中或未被选中时才能获得,无法获得所有现有复选框的状态:

if ($('#opcion'+product_id).is(":checked"))
{
// it is checked
alert ('checked');
}
else
{
alert ('unchecked');
}

我知道很难理解我在问什么,但请耐心等待,并询问有关我问题的更多详细信息。

在jquery中使用.class选择器而不是#id选择器将返回所有匹配的元素。所以,在你所有的复选框上,像这样添加一个特殊的类:

'<input type="checkbox" id="opcion'.$row["id"].'"  class="opcion_cbox"  value="">'

这样,当您使用jquery执行以下操作时,您将使用所有选中的复选框:

var checked_boxes = $(".opcion_cbox:checkbox:checked");

或者你可以沿着这条线循环浏览它们:

$(".opcion_cbox:checkbox:checked").each(function(){
// do something with $(this) checkbox in a loop
});

如果您想获得"行id",以便可以引用具有该id的页面上的其他元素,可以使用data-variables

'<input type="checkbox" 
id="opcion'.$row["id"].'" 
class="opcion_cbox" 
data-rowid="'. $row["id"] .'" 
value="">'

$(".opcion_cbox:checkbox:checked").each(function(){

var rowid = $(this).data('rowid');
var nombre = $("#opcion_nombre"+ rowid).val();
var precio = $("#opcion_precio"+ rowid).val();
});

或者更干净地说,您可以将这些值本身作为数据变量从php:中放入

'<input type="checkbox" 
id="opcion'.$row["id"].'" 
class="opcion_cbox" 
data-nombre="'. $row_opc["nombre"] .'" 
data-precio="'. $row_opc["precio"] .'" 
value="">'
// be sure to escape any double-quotes in nombre/precio

$(".opcion_cbox:checkbox:checked").each(function(){

var nombre = $(this).data('nombre');
var precio = $(this).data('precio');
});

从那里开始,您可以对每个复选框进行各种排序。如果你愿意,你可以在所有选项框上循环,如果框被选中,则在循环内部进行检查(如果你需要对所有框执行某些操作,而不仅仅是对选中的框(。

最新更新