在验证Jquery后从表格中获取值

4

我有一张简单的表格:

<form id="form" method="POST" action="">                   
    <input type="text" name='first_name' data-validation="alphanumeric" />              
    <select name='custom_quantity'>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
    </select>
</form>

使用简单的验证脚本:

<script>
    (function($, window) {    
        var dev = '.dev'; // window.location.hash.indexOf('dev') > -1 ? '.dev' : '';

        window.applyValidation = function(validateOnBlur, forms, messagePosition) {
            if ( !forms )
                forms = 'form';
            if ( !messagePosition )
                messagePosition = 'top';

            $.validate({
                form : forms,
                validateOnBlur : validateOnBlur,
                errorMessagePosition : messagePosition,
                scrollToTopOnError : true,
                onSuccess : function($form) {                                                           
                    first_name = $( "#form input[name='first_name']" ).val();
                    select = $( "#form select[name='custom_quantity']" ).val();
                    alert( "Your: " + first_name + ' ' + select +  " values." );

                    return false;
                }
            });
        };

        window.applyValidation(true, '#form', 'element');
    })(jQuery, window);
</script>

验证功能正常,但在输入框中输入一些文本并从选择标签中选择一个值后,我没有收到警报消息。

此外,Firebug控制台不显示任何错误。

这种从表单获取值的方式是否有效?

first_name = $( "#form input[name='first_name']" ).val();

“#form……你尝试过从选择器中删除前导空格吗?” - ADyson
1个回答

0
first_name = $( "#form input[name='first_name']" ).val();
select = $( "#form select[name='custom_quantity']" ).val();

我认为此行返回一个数组,请尝试选择第一个元素

first_name = $( "#form input[name='first_name']" )[0].val();                    
select = $( "#form select[name='custom_quantity']" )[0].val();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接