【问题标题】:How can I change the search form to separate single digits fields?如何更改搜索表单以分隔单个数字字段?
【发布时间】:2022-01-01 15:28:11
【问题描述】:

我有一个多字符搜索字段,它通过 AJAX 获取 WoocCommerce 产品数据并正常工作。

当前搜索字段

我需要将我的搜索字段拆分为单个 6 位数字框,这是我可以使用的表单示例:

<form>
     <input type="number" name="abc" min="0" max="9">
      <input type="number" name="abc" min="0" max="9">
      <input type="number" name="abc" min="0" max="9">
      <input type="number" name="abc" min="0" max="9">
      <input type="number" name="abc" min="0" max="9">
      <input type="number" name="abc" min="0" max="9">
        </form>

如何将我的搜索字段的字符串拆分为多个单个数字的搜索字段?

我的前端代码

<input type="text" name="keyword" id="keyword" onkeyup="fetch()">
<div id="datafetch">Your numbers will show here</div>
<script>
function fetch(){
    $.post('<?php echo admin_url('admin-ajax.php'); ?>', 
    {'action':'my_action'},
    function(response){
        $('#datafetch').append(response);
        console.log(result);
    });
}
</script>

Functions.php 中的代码

add_action('wp_ajax_data_fetch' , 'data_fetch');
add_action('wp_ajax_nopriv_data_fetch','data_fetch');
function data_fetch(){
   $myquery = esc_attr( $_POST['keyword'] );
}

【问题讨论】:

  • 如果我正确理解您的要求 - 只需将六个输入字段连接成一个值并提交即可。 (六个输入字段应该有不同的ID,否则系统无法知道如何进行连接)

标签: php html woocommerce


【解决方案1】:

使用您建议的脚本,但在每个输入字段中放置标签索引:

然后做一些用户体验任务:

  • 由于您指定 type='number' 那么您不能使用 maxlength 属性来防止每个输入超过 1 位的输入,因此您必须在您的 js 中处理它。
  • 让用户可以通过按退格键返回上一个输入
  • 如果用户手动关注当前输入值并按另一个数字,则替换当前输入值
  • 做一些 UI 工作!

现在您已经有了用户友好的分隔输入框。但是您必须在提交给 WordPress admin-ajax 以传递给您的函数之前集成它们的输入值,或者您可以单独发送它们并将它们集成到服务器端函数中,做你想做的事。

这里是演示:

function fetch(){
    $('#keyword').val() = $('#input1').val() + 
                          $('#input2').val() + 
                          $('#input3').val() + 
                          $('#input4').val() + 
                          $('#input5').val() + 
                          $('#input6').val() ; 
   //rest of your function
}

$(function() {

      $("input").keydown(function (e) {
        if(e.keyCode == 8) {
           $(this).prev().focus();
        }
        if (this.value.length == this.maxLength) {
          $(this).val('');
        }
    });
  
    $("input").keyup(function () {
        if (this.value.length == this.maxLength) {
          $(this).next().focus();
        }
    });
  
});
input {  
    height: 21px;
    background: #f1f1f1;
    border: 1px #c9c9c9 solid;
    border-radius: 3px;
    -moz-appearance: none;
    appearance: none;
    text-align: center;
    }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    
<form>
     <input id="input1" type="number"  min="0" max="9" tabindex="1"  maxlength="1">
     <input id="input2" type="number"  min="0" max="9" tabindex="2"  maxlength="1">
     <input id="input3" type="number"  min="0" max="9" tabindex="3"  maxlength="1">
     <input id="input4" type="number"  min="0" max="9" tabindex="4"  maxlength="1">
     <input id="input5" type="number"  min="0" max="9" tabindex="5"  maxlength="1">
     <input id="input6" type="number"  min="0" max="9" tabindex="6"  maxlength="1">
 </form>

【讨论】:

  • 谢谢。我已经尝试输入如图所示,它还没有为我工作。最大长度未应用于每个框。
  • 每个字段的最大长度将由 js 处理,正如我所说。你的js加载正确吗?你确定它没有缓存吗?答案 sn-p 在您的浏览器中有效吗?
  • 谢谢。我可以适应这个返回位置结果吗?例如,如果我搜索 _ _ _ _ _ 1,它将只显示最后一个位置为 1 的结果。
  • 当你在 s 参数中给出一个值(在你的代码中:'s' => esc_attr( $_POST['keyword'] )) WordPress 将搜索任何具有 s 子字符串的字符串帖子标题和内容。因此,如果您只需要获取完全覆盖的名称,则必须在将其发送到 ajax 之前将空输入替换为 0。
猜你喜欢
  • 1970-01-01
  • 2020-03-10
  • 2012-07-19
  • 1970-01-01
  • 2015-09-23
  • 2010-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多