如何以html格式将多个值附加到单个参数?

时间:2012-12-11 23:30:43

标签: php jquery html

假设我有以下表格:

<form action="process.php">
<input type="checkbox" name="option1" value="oats"> Oats
<input type="checkbox" name="option2" value="beans"> Beans
<input type="hidden" name="parameter" value="a"/>
<input type="submit" value="Submit"/>
</form>

通常会发生什么,点击网址后,浏览器会重定向到:

process.php?option1=oats&option2=beans&parameter=a

如何使点击提交时所有复选框最终都作为“参数”的一部分,但用逗号分隔?换句话说,它将是:

process.php?parameter=a,oats,beans

如果没有html解决方案,最好使用最少的javascript / jquery / html解决方案。

4 个答案:

答案 0 :(得分:13)

如果您想在一个参数中使用多个值,则必须将其命名为parameter[]

f.e:

<form action="process.php">
<input type="checkbox" name="parameter[]" value="oats"> Oats
<input type="checkbox" name="parameter[]" value="beans"> Beans
<input type="hidden" name="parameter[]" value="a"/>
<input type="submit" value="Submit"/>
</form>

答案 1 :(得分:1)

有一种非常有用的方法可用于动态数据分组。

<form action="file.php" method="post">

<?php for ( $i = 0; $i < count( $something ); $++ ) { ?>
    <input type="checkbox" name="<?php $something[$i]; ?>[]" value="user_id"> Oats
    <input type="checkbox" name="<?php $something[$i]; ?>[]" value="user_name"> Beans
<?php } ?>
<input type="submit" value="Submit"/>
</form>

答案 2 :(得分:0)

这里或多或少的想法: 第一种形式,1隐藏输入将捕获所有第二和无效形式的vaules

<script type="text/javascript">
function myfunc(){
    $('#void input').each(function(id,elem){
         b = $("#res").val();
         if(b.length > 0){
            $("#res").val( b + ',' + $(this).val() );
        } else {
            $("#res").val( $(this).val() );
        }

    });
        alert("VAL "+$("#res").val());
    $("#real").submit();

return false;
}

</script>

<form id="real" action="process.php">
<input id="res" type="hidden" name="parameter" value=""/>
</form>

<form id="void">
<input type="checkbox" name="option1" value="oats"> Oats
<input type="checkbox" name="option2" value="beans"> Beans
<input type="hidden" name="parameter" value="a"/>
<input type="submit" value="Submit" onClick="javascript:myfunc()"/>
</form>

添加一些修复,提交正确的表单。在jsfiddle上测试:

JsFiddel "working" example

添加单个表格版本

您必须知道该字段的名称,并忽略答案中的其他参数

<script type="text/javascript">
function myfunc(){
    // can use more selector
    $('#real input').each(function(id,elem){
        // append the data to this field so no need to process it
        if(this.id == 'res'){
            return;                
        }
        // here I concat the values 
        b = $("#res").val();
        if(b.length > 0){
            $("#res").val( b + ',' + $(this).val() );
        } else {
            $("#res").val( $(this).val() );
        }

    });
    alert("VAL "+$("#res").val());  // remove me
    $("#real").submit();

    return false;
}

</script>

<form id='real' method='POST' action="#">
<input type="checkbox" name="option1" value="oats"> Oats
<input type="checkbox" name="option2" value="beans"> Beans
<input id="res" type="hidden" name="parameter" value="a"/>
<input type="submit" value="Submit" onClick="javascript:myfunc()"/>
</form>​

答案 3 :(得分:0)

我真的只对实际检查的复选框感兴趣 - 所以我从其他示例构建了myfunc()

function myfunc(){
    $('#void input[type="checkbox"]').each(function(id,elem){
        if ( ! $(this).is(':checked') ) return;
        b = $("#res").val();
        if(b.length > 0){
            $("#res").val( b + ',' + $(this).val() );
        } else {
            $("#res").val( $(this).val() );
        }
    });
    var stuff = $("#res").val();
    if ( stuff.length < 1 ) {
        alert('Please select at least one');
    } else {
        $("#real").submit();
    }
}