获取输入值不起作用?

时间:2012-03-13 16:24:12

标签: jquery

我试图通过以下方式获取输入的值:

var email = $('form.new-user-form').find('[class=email]').val();
console.log(email);

表单是使用PHP生成的,但我不认为问题是因为这样,因为几天前它运行得很好,看起来像这样:

$form = array(
'<form name="user-count-form" class="user-count-form" method="post" action="#">',
    '<fieldset class="user-count-fields">',
        '<fieldset class="display-count-wrapper">',
            '<label for="display-count" class="display-count-label">Users</label>',
            '<input type="text" name="display-count" class="display-count" value="' . $this->userCount() . '" autocomplete="off" readonly="readonly" />',
        '</fieldset>',
        '<fieldset class="new-user-wrapper">',
            '<button type="submit" name="new-user" class="new-user">New User</button>',
        '</fieldset>',
    '</fieldset>',
'</form>',
'<form name="new-user-form" class="new-user-form" method="post" action="#">',
    '<fieldset class="new-user-fields">',
        '<fieldset class="email-wrapper">',
            '<label for="email" class="email-label">Email</label>',
            '<input type="email" name="email" class="email" value="" autocomplete="off" />',
        '</fieldset>',
        '<fieldset class="create-wrapper">',
            '<button type="submit" name="create" class="create">Create</button>',
        '</fieldset>',
    '</fieldset>',                         
'</form>',
'<div class="message-handling"></div>'
);
$form = implode("", $form);
echo $form;

如果输入为空,我进入控制台的所有内容都是(an empty string)。为什么会这样?

1 个答案:

答案 0 :(得分:3)

尝试在类值周围添加引号,或对类使用.表示法:

var email = $('form.new-user-form').find('[class="email"]').val();
console.log(email);

或:

var email = $('form.new-user-form').find('.email').val();
console.log(email);