我想知道如何将数组的值与textarea匹配,如果不匹配,它会显示一条错误消息,例如“您确定输入文本字段有什么问题吗?”
function populateVariable(appLang){
var selector = $('#field_new');
var variables = [];
// var exists;
//input select
selector.children('div').each(function(id, el) {
$(el).children('.sub_block').each(function (id, el) {
variables.push("$" + $(el).children('input')[0].value + "$ ")
})
});
selector.children('div').each(function(id, el) {
$(el).children('.option-item').each(function (id, el) {
variables.push("$" + $(el).children('input')[0].value + "$ ")
})
});
$('#valueInput').text(appLang.getKey('valueInput') + variables);
}
<div class="" id="formulaire-preview-text">
<div id="valueInput"></div><br>
<div id="inputs"></div><br>
<textarea id="formulaire-preview-textarea" rows="12" cols="60"></textarea>
</div>
此函数获取输入的所有值并将它们放在数组“变量”中
答案 0 :(得分:-1)
使用Array.prototype.includes()
var array1 = [1, 2, 3];
console.log(array1.includes(2));
// expected output: true
var pets = ['cat', 'dog', 'bat'];
console.log(pets.includes('cat'));
// expected output: true
console.log(pets.includes('at'));
// expected output: false
请参见https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Global_Objects/Array/includes
答案 1 :(得分:-1)
如果要检查,请在填充数组后(通过选择或输入,这就是您在代码中所做的事情),如果文本区域的值位于数组中,则必须执行此操作:
var myArr = ['x','hallo','world','js','2018','hi', 'text','yourvalues','select','blabla']
//inArray is good becouse you get and can save the position in the array
$('#formulaire-preview-textarea').on('input', function (evt) {
var value = evt.target.value
if ( $.inArray(value,myArr) >= 0 ){
$('#error').text(value+ " in array");
}else{
$('#error').text(value + " not in array, something wrong Are you sure about what you got into the text field");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="error">
</div>
<div class="" id="formulaire-preview-text">
<div id="valueInput"></div><br>
<div id="inputs"></div><br>
<textarea id="formulaire-preview-textarea" rows="12" cols="60"></textarea>
</div>