我有这样的表格:
<input type="text" class="Qty form-control input-sm" name="Qty" value="0">
<input type="text" class="Qty form-control input-sm" name="Qty" value="0">
<input type="text" class="Qty form-control input-sm" name="Qty" value="0">
<input type="text" class="Qty form-control input-sm" name="Qty" value="0">
用户将输入框更新为随机值。
在jquery上我这样做:
var TEST = $('.Qty').serialize();
在PHP中,输出是(如果只有第一个输入设置为5:
$TEST = $_POST['TEST'];
echo $TEST;
// OUTPUT: Qty=5&Qty=0&Qty=0&Qty=0
如何才能获得非0的值?
// OUTPUT: Qty=5
我试过了:
var TEST = $('.Qty').not('[value="0"]').serialize();
但它不起作用。任何想法?
答案 0 :(得分:0)
var arr = new Array();
$('.Qty').each(function() {
if ( $('.Qty').attr("value") != "0") {
arr.push($('.Qty').attr("value"))
}
});
arr.serialize();
答案 1 :(得分:0)
很抱歉,但我担心您完全误解了通过HTTP将参数/值传递到服务器端脚本的概念。
$i = 0;
while (/*condition*/) {
echo '<input type=text class=Qty name=parameter_'.$i.'>'
$i++;
}
<input type="text" class="Qty" name="parameter_1" value="value_1">
<input type="text" class="Qty" name="parameter_2" value="value_2">
<input type="text" class="Qty" name="parameter_3" value="value_3">
<input type="text" class="Qty" name="parameter_4" value="value_4">
<input type="text" class="Qty" name="parameter_5" value="value_5">
sendThisToServer1 = $('.Qty').serialize();
$(".Qty").each(function() { sendThisToServer2[this.name] = $(this).val(); });
$first = $_POST['sendThisToServer1'];
$second = $_POST['sendThisToServer2'];
echo $first; // parameter_1=value_1¶meter_2=value_2¶meter_3=value_3¶meter_4=value_4
echo $second; // { parameter_1:values_1,parameter_2:value_2,parameter_3:value_3,parameter_4:value4 }
答案 2 :(得分:0)
我认为某些验证和检查应该在服务器端,因为 客户端验证很容易被“不太好”的访问者“忽略”。
<强> HTML:强> ID应该是唯一的,并且只在页面中出现一次。 将名称更改为“Qty []”将在服务器端“创建”一个数组。
<input type="text" class="Qty form-control input-sm" name="Qty[]" id="Qty1" value="0">
<input type="text" class="Qty form-control input-sm" name="Qty[]" id="Qty2" value="0">
<input type="text" class="Qty form-control input-sm" name="Qty[]" id="Qty3" value="0">
<强> JS:强>
同样。
<强> PHP:强>
$test = $_POST['TEST'];
foreach($test as $k => $v)
{
if($v != 0)
{
// It's not 0, what do you want to do?
//....
//....
}
}
答案 3 :(得分:0)
您需要在HTML
中更改以下代码<input type="text" class="Qty form-control input-sm" name="Qty[]" value="0">
<input type="text" class="Qty form-control input-sm" name="Qty[]" value="0">
<input type="text" class="Qty form-control input-sm" name="Qty[]" value="0">
<input type="text" class="Qty form-control input-sm" name="Qty[]" value="0">
当您单击提交时,您可以通过以下PHP代码获取值。
$qty = $_POST['Qty'];
for($i=0;$i<count($qty);$i++)
{
if($qty[$i]!='0')
{
echo 'Valid value';
}
else
{
echo 'Invalid value';
}
}
答案 4 :(得分:0)
这就是你想要的.....一个自定义过滤器功能将根除值为零的项目。
$('.Qty').filter(function() {
return parseInt(this.value, 10) !== 0;
}).serialize()