jQuery可以根据所有数字或字母的输入触发不同表单的提交

jQuery to trigger submitting of different forms based on input being all numbers or just letters

本文关键字:输入 提交 表单 数字 jQuery      更新时间:2023-09-26

我已经尝试了几个小时了。要么我的表格只有一个有效,要么根本没有。我无法获取表单函数或提交函数来执行任何操作。它只是禁用了整个东西,并且不提交。感谢您的帮助。

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

如果"s-webref"的输入都是数字,请提交第一个表单:"property webref search"。

如果没有,请提交第二份表格"财产搜索"。

我的第一张表格(位于顶部):

        <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();
    }
});