javascript - jQuery 根据输入全是数字或只是字母来触发不同表单的提交

标签 javascript jquery html forms submit

我已经尝试这样做几个小时了。我的表格要么只有一种有效,要么根本无效。我无法让表单函数或提交函数执行任何操作。它只是禁用整个事情并且不提交。如有任何帮助,我们将不胜感激。

基本上,我想根据第一个表单的输入字段“s-webref”中的条目提交两个表单。

如果“s-webref”的输入全部为数字,请提交第一个表单:“property-webref-search”。

如果没有,请提交第二个表单“属性(property)搜索”。

我的第一个表单(位于顶部):

        <form name="property-webref-search" id="property-webref-search" method="get" action="<?php bloginfo('url'); ?>/">

            <input type="text" class="text webref" id="s-webref" name="s" value="<?php _e('İlan no veya arama', 'woothemes'); ?>" onfocus="if (this.value == '<?php _e('İlan no veya arama', 'woothemes'); ?>') {this.value = '';}" onblur="if (this.value == '') {this.value = '<?php _e('İlan no veya arama', 'woothemes'); ?>';}" />

        <input type="submit" class="submit button" name="property-search-webref-submit" id="property-search-webref-submit" value="<?php _e('ara', 'woothemes'); ?>" /> 

        </form>

我的第二种形式:

        <form name="property-search" id="property-search" method="get" action="<?php bloginfo('url'); ?>/">

        <input type="text" class="main-query text" id="s-main" name="s" value="<?php if ( $keyword != '' ) { echo $keyword; } else { _e('Arama...', 'woothemes'); } ?>" onFocus="if (this.value == '<?php _e('Arama...', 'woothemes') ?>') {this.value = '';}" onBlur="if (this.value == '') {this.value = '<?php _e('Arama...', 'woothemes') ?>';}" />
                <input class="view-button" type="submit" value="<?php _e('ara', 'woothemes') ?>" name="property-search-submit" />

        </form>

这是我使用的 javascript,它使表单根本不起作用:

$('#property-search-webref-submit').click(function() {
var searcherz = $("input#s-webref").val();
if(searcherz.match(/^\d+$/)) {
$('form#property-webref-search').submit();
}
else {
$('form#property-search').submit();
}});

最佳答案

表单始终处于提交状态,因为您没有阻止提交按钮的默认操作:

$('#property-search-webref-submit').click(function(e) {
    e.preventDefault();
    var searcherz = $("#s-webref").val();
    if( /^\d+$/.test(searcherz) ) {
        $('#property-webref-search').get(0).submit();
    } else {
        $('#property-search').get(0).submit();
    }
});

关于javascript - jQuery 根据输入全是数字或只是字母来触发不同表单的提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14917372/

相关文章:

JavaScript 继承与工作代码助手和大纲?

javascript - 如何在 Angular 中选择所需的列表?

javascript - 在 jstree 中的两棵树之间拖放

javascript - ReactJS/jQuery 获取 HTML

html - 有没有办法在检查元素后复制出现在 Chrome 开发工具中的路径?

javascript - 帐户密码电子邮件 token 是否会过期?

javascript - 通过 JS 代码禁用元素 - 不起作用

jquery - 类似亚马逊的界面,用于选择产品尺寸和颜色(即单击一个小红色框来选择红色产品等)

php - 在一张登录表单上登录两个不同的区域

javascript - 如何检测用户是否运行 IE 6?