我正在尝试捕获我的输入字段的值,并在我的表单类中有以下方法来实现:
getData: function() {
var fields = ['name', 'email', 'message'];
$.each(fields, function(index, el) {
form.data[el] = $('#'+el).val();
console.log(form.data[el]);
});
},
(不用管“select”字段,那是一个自定义的选择框,我将需要以不同的方式处理它) 我在表单改变事件上执行捕获。输入字段都有适当的ID,看起来像这样:
(nevermind the 'select' field, that's a custom made select box and I'll have to handle that one differently) 我在表单更改事件上执行捕捉。 输入字段都有正确的ID,看起来像这样:
<input type="text" id="name">
<input type="email" id="email">
<textarea name="message" id="message"></textarea>
现在,当我在这些字段中输入内容时,我只能从文本区域获得值,而其他输入字段则返回未定义。
我还要注意到,这个表单是通过Ajax加载的,但由于它通常会捕获字段的更改事件,所以我怀疑这不是问题的原因。
name
属性进行映射。无论如何,用自己的答案关闭问题。 - dreamweiver