使用括号在复选框名称中使用php和javascript? [英] use brackets in checkbox name when using php and javascript?

查看:200
本文介绍了使用括号在复选框名称中使用php和javascript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有如下复选框的表单:

i have a form with checkboxes like this:

    <input type="checkbox" name="type[]" value="1" />Fast Food<br>
    <input type="checkbox" name="type[]" value="2" />Table Service<br>
    <input type="checkbox" name="type[]" value="3" />Cafeteria<br>

当我使用名称(类型[])中的括号时,我的php工作:

when i use the brackets in the name (type[]), my php works:

    $type=$_POST['type'];

    echo "types are:";
for ( $counter = 0; $counter < sizeof($type); $counter += 1) {
    echo "<br>".$type[$counter];
}

但我的javascript无法使用:

but my javascript doesn't work:

    var f = document.addform;
    for (var i=0;i<f.type.length;i++){
        if(f.type[i].checked==true){
            break;
        }
        if(i==(f.type.length-1)){
            alert("No categories entered!");
            valid=false;
        }
    }

p>

however, if i take away the brackets:

<input type="checkbox" name="type" value="1" />Fast Food<br>

那么PHP不起作用,但是javascript不起作用。

then the PHP doesn't work but the javascript does.

这里发生了什么?我应该使用什么?

what's going on here? what should i use?

感谢。

推荐答案

用于处理具有相同名称的多个表单控件的异常系统,它期望名称包括 [] ,但它不在变量名中使用它们。

PHP has an unusual system for handling multiple form controls with the same name, it expects the names to include [] but it doesn't use them in the variable name.

JavaScript没有这个问题。

JavaScript doesn't have that issue. The property will still have the brackets.

当然,方括号在JS中有特殊的含义,所以不能使用点符号来访问属性。

Of course, square brackets have special meaning in JS, so you can't use dot notation to access the property.

f['type[]'][i].checked

这篇关于使用括号在复选框名称中使用php和javascript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆