【问题标题】:How to check whether the content of the field contains part of the second field?如何检查字段的内容是否包含第二个字段的一部分?
【发布时间】:2019-01-19 04:21:48
【问题描述】:

我有一个代码可以检查两个字段是否具有相同的值:

$ampD.blur(function() {
      setInterval(update, 1000);

      function update() {
        if ($('input#autocompletegoogle').attr('value') == $('input#field-address').attr('value')) {
          $ampD.attr('style', '');
        } else {
          $ampD.attr('style', 'border: 1px solid #f00!important;background: #ffefef !important');
        }

但有时$('input#autocompletegoogle') 包含附加标记示例:

"5611JM Eindhoven, Netherlands"

但 input#field-address 包含 "Eindhoven, Netherlands" 并且我得到错误。

如何检查 $('input#autocompletegoogle') 是否包含来自 $('input#field-address') 的值?

我试图找到解决方案将近一小时...我可以找到正确的信息。有人可以帮助我吗?

【问题讨论】:

标签: jquery


【解决方案1】:

只需修改您的if 条件以使用includes() 之类的东西,而不是==

var fieldAddress = $('input#field-address').val().toLowerCase();
var autoCompleteGoogle = $('input#autocompletegoogle').val().toLowerCase();

if (autoCompleteGoogle.includes(fieldAddress)) {
    //Auto Complete Google contains Field Address
}

如果您希望它保持区分大小写,可以从每个中删除 toLowerCase()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多