我已经尝试这样做几个小时了。我的表格要么只有一种有效,要么根本无效。我无法让表单函数或提交函数执行任何操作。它只是禁用整个事情并且不提交。如有任何帮助,我们将不胜感激。
基本上,我想根据第一个表单的输入字段“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/