在提交时将数据绑定到表单



我有一些形式:

<form name="myForm" ... >

我像这样选择它:

 document.forms["myForm"]

提交时,我想在表单中添加几个键值对。 这些键值对不需要在调用submit()函数之前采用这种形式。 我像这样添加对:

document.forms["myForm"][key] = value;

但是,这不起作用。 它说我有一个意想不到的[. 将数据绑定到表单的适当方法是什么?

键、值对示例:

key = "PresentLevelsAssessment" + selected[i].id;
value = selected[i].getAttribute('data-type');

试试这个:

document.forms.myForm.elements.key.value = ****anyvalue****;

所以我无法弄清楚为什么它不起作用,所以我只是解决了它。 我想根据是否选中某些选项来绑定数据。 我没有在提交时绑定数据,而是在选中选项时将 html 写入表单。

$('.check').on('click', function() {
    if ($(this).prop('checked')) {
        var html = '<div style="display:none" id="Info'+ $(this).attr('id') +'">' +
            '<input name="PresentLevelsAssessment'+ $(this).attr('id') +'" value="'+ $(this).attr('data-type') +'" />' +
            '<input name="PresentLevelsDate'+ $(this).attr('id') +'" value="'+ $(this).attr('data-date') +'" />' +
        '</div>';
        form.prepend(html);
    } else {
        $('#Info' + $(this).attr('id')).remove();
    };      
});

最新更新