将按钮的ID传递给表单

时间:2014-08-12 14:00:18

标签: javascript jquery html

我找到了这个pass id to jquery modal form,但是看到我不太懂jQuery,我不知道PhP我不确定如何在我的例子中实现这个。

我的目标是能够在按下按钮后将按钮的ID传递给表单。像这样:

<button id="dynamic_id">
<form id="time_form" action="{% url 'event' pk='button_id' %}" method="post">
# Lots of stuff
</form>

button_id是我想要按钮ID的地方。

我想到了像

这样的东西
function getId(button_id) {
    document.getElementById('time_form').action = "{" + "%" + "url " + "event" + ' pk="' + button_id + '" %" + "}"';
}

<button id="dynamic_id" onClick="getId(this.id);">

<form id="time_form" action=""> (...)
</form>

但这不起作用。我收到一条错误消息

400 Bad Request: Your browser semt a request this server cannot understand

更新

我不是在尝试这个:

function getId(button_id) {
    var vk_input = document.CreateElement("input");
    vk_input.name = "vk_id";
    vk_input.value = button_id;
    vk_input.type = "hidden";
    document.getElementById("time_form").appendChild(vk_input);
}

但由于某种原因它不起作用。怎么了?

4 个答案:

答案 0 :(得分:1)

看看这个:

<script>
$(document).ready(function(){
    $('#dynamic_id').click(function(){
        var id=$(this).attr('id');
        var array_ids = [];
        $('.dinamics').each(function(){
            //console.log('gdfg');
            array_ids.push($(this).attr('id'));
        });
        for(var i = 0; i < array_ids.length; i++){
            $('#time_form').append('<input type="hidden" name="your_button_id_'+i+'" value="'+array_ids[i]+'"/>')
        }
        $('#time_form').submit();
    })
})
</script>

答案 1 :(得分:0)

用jquery怎么样。

<button id="dynamic_id">
<form id="time_form" action="" method="post">
</form>

<script>
$(document).ready(function(){
    $('#dynamic_id').click(function(){
        var id=$(this).attr('id');
        $('#time_form').append('<input type="hidden" name="your_button_id" value="'+id+'"/>')
        $('#time_form').submit();
    })
})
</script>

答案 2 :(得分:0)

Button id正确传递给getId(),但生成的字符串似乎没有效果,这里引号有些问题。请参阅jsfiddle

上的简化示例

试试这个:

document.getElementById('time_form').action = "{" + "%" + "url " + "event" + ' pk="' + button_id + '" ' + "%" + "}";

答案 3 :(得分:0)

我推荐你使用jquery,干净效果更好。 这就是你想要的?

<button id="dynamic_id" class="dinamics">Lots of stuff</button>
<button id="dynamic_id_1" class="dinamics">1</button>
<button id="dynamic_id_2" class="dinamics">2</button>
<button id="dynamic_id_3" class="dinamics">3</button>
<button id="dynamic_id_4" class="dinamics">3</button>
<button id="dynamic_id_5" class="dinamics">3</button>
<button id="dynamic_id_6" class="dinamics">3</button>
<button id="dynamic_id_7" class="dinamics">3</button>
<form id="time_form" action="" method="post">
</form>

<script>
$(document).ready(function(){
    $('#dynamic_id').click(function(){
        var array_ids = [];
        $('.dinamics').each(function(){
            array_ids.push($(this).attr('id'));
        });
        for(var i = 0; i < array_ids.length; i++){
            $('#time_form').append('<input type="hidden" name="your_button_id_'+i+'" value="'+array_ids[i]+'"/>')
        }
        $('#time_form').submit();
    })
})
</script>