2015-10-18 40 views
1

我想,要检查我的textarea的价值几乎是一样的,需要例如相同:如何检查是否文本几乎是必需的

我有一个HTML代码:

<textarea class="ab" placeholder="Type here"></textarea> 
<div class="result"> 
</div> 

和jQuery代码:

$(document).ready(function(){ 

    $(".btn").click(function(){ 
    var a = $(".ab").val(); 
    var b = $(".result").html(); 
    /* */ 



if(a.indexOf('Which of the following is generally true about Open Source software?') >= 0){$('.result').html('Its usually developed on a volunteer basis and anyone is free to change it (correct).');} /* */ 
    else{ 
     $(".result").html("error"); 

    } 
    }); 

}); 

此代码不起作用正如我真正想要的,这正是我试图使。但我想的东西是例如当$('.ab')的值几乎一样的文字Which of the following is generally true about Open Source software?the following is generally truetrue about the Open Source,在$(".result")仍然有HTML作为Its usually developed on a volunteer basis and anyone is free to change it (correct).

让我怎么做,谢谢您的帮助

+1

没有简单的解决办法,这样做我研究模糊搜索。问题过于宽泛,没有更精确的搜索标准 – charlietfl

+0

如果十个输入单词中的五个匹配字符串,做什么要求? – guest271314

回答

0

其实应该是:

$(document).ready(function(){ 
     $(".btn").click(function(){ 
      var a = $(".ab").val(); 
      var b = $(".result").html(); 
      var c = 'Which of the following is generally true about Open Source software?'; 
      console.log(c.indexOf(a)); 
      if(c.indexOf(a) >= 0){ 
       $('.result').html('Its usually developed on a volunteer basis and anyone is free to change it (correct).'); 
      } else { 
       $(".result").html("error"); 
      } 
     }); 

    }); 

<textarea class="ab" placeholder="Type here">following is generally true about Open Source</textarea> 
<div class="result"></div> 
<button class="btn">test</button> 
+0

当我用字符串测试时,它对我不起作用:“以下哪项通常是”,它不会返回结果“它通常是在志愿者基础上开发的,任何人都可以自由地更改它(正确) 。“ [jsbin](http://jsbin.com/kayibe/edit?html,css,js输出) – user3722325

+0

它返回”它通常是在志愿者基础上开发的,任何人都可以自由改变它(正确)。“为了我。 –

1

尝试将输入文本分割成数组,使用$.each()迭代输入字,如果输入的字匹配所选短语中的至少五个单词,则返回true,否则在if处返回false;例如。;请尝试输入或粘贴在textarea

the following is generally truetrue about the Open Source

$(document).ready(function() { 
 

 
    $(".btn").click(function() { 
 
    var a = $(".ab"); 
 
    var b = $(".result"); 
 
    var arr = a.val().split(/\s+/); 
 
    var n = 0; 
 
    var phrase = "Which of the following is generally true about Open Source software?"; 
 
    $.each(arr, function(key, val) { 
 
     if(phrase.indexOf(val) >= 0) ++n; 
 
    }) 
 
    
 
    if (n >= 5) { 
 
    b.html('Its usually developed on a volunteer basis and anyone is free to change it (correct).'); 
 
    } 
 
    else { 
 
     b.html("error"); 
 
    }; 
 
    a.val(""); n = 0; 
 
    }); 
 

 
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 

 
<textarea class="ab" placeholder="Type here"></textarea> 
 
<div class="result"></div> 
 
<button class="btn">click</button>

+0

嗯,谢谢,但它实际上不适用于字符串“以下一般是真实的”。 – user3722325

+0

@ user3722325“下面一般是真的”是四个字,试着用“以下一般是真的”?或者,将'if'条件调整为'n> = 4'。请注意,递增匹配计数时,不检查前面的单词是否是当前单词;即“真实真实真实真实”可能会返回“真实”;虽然这部分可以调整,以测试不同词的匹配 – guest271314

+0

@ user3722325另请参阅http://stackoverflow.com/questions/23305000/javascript-fuzzy-search-that-makes-sense/ – guest271314