2014-12-30 32 views
0

我有某種形式:數據綁定到表單提出

<form name="myForm" ... > 

我選擇它,如下所示:

document.forms["myForm"] 

在提交時,我想幾個鍵值對添加到表格。這些鍵值對不需要在調用submit()函數之前的形式。我添加這樣的對:

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

但是,這是行不通的。它說我有一個意想不到的[。將數據綁定到表單的適當方式是什麼?關鍵的

例,值對:

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

是你的整個代碼?它看起來很好。 – Scimonster

+0

@Scimonster Theres更多一點,但這是它的精神。我絕對遵循這裏顯示的標準。 –

+0

如果使用'document.forms [「myForm」]。元素[key] .value = value;'? – augustoccesar

回答

0

所以我無法弄清楚爲什麼它不工作,所以我就在它的工作。我想根據是否檢查某些選項來綁定數據。而不是在提交時綁定數據,而是在檢查選項時將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(); 
    };  
}); 
0

試試這個:

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

只有代碼答案是不鼓勵的。除了提供功能正常的代碼之外,最好解釋爲什麼和如何。 – RubberDuck