[英]get changing value from readonly textarea by javascript
I have a readonly
textarea which its value keep changing. 我有一个
readonly
textarea,它的价值不断变化。 Now I want the page keeps updating its value and return the result if it matches with the provided string or not. 现在我希望页面不断更新其值并返回结果,如果它与提供的字符串匹配。 Here is what I tried but it's not working
这是我尝试过但它不起作用
var string = "This is my house";
$('#textarea').bind('input', function() {
if ($(this).val() === string.trim()) {
$('#message').html('matching').css('color', 'green');
} else $('#message').html('not matching').css('color', 'red');
});
I think you can't listen to the input
event as the textarea is readonly
, what you can do though is trigger the input
- or any other event - using .trigger()
in same code that changes the text content of the textarea 我认为你不能听取
input
事件,因为textarea是readonly
,你可以做的虽然是触发input
- 或任何其他事件 - 在相同的代码中使用.trigger()
来改变textarea的文本内容
var string = "This is my house", ta = $('#textarea'), msg = $('#message'), newContent = 'just dummy text'; // replacement text, change it to see the result ta.on('input', function() { if ($(this).val() === string.trim()) { msg.html('matching').css('color', 'green'); } else { msg.html('not matching').css('color', 'red'); } }); $('#btn').on('click', function() { ta.text(newContent).trigger('input'); // trigger input event as text content is changed });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script> <button id="btn">Change it</button><br> <textarea readonly name="" id="textarea" cols="30" rows="5">This is my house</textarea> <hr><div id="message"></div>
You could do something like this 你可以这样做
var string = 'This is my house'; var array = ['Lorem ipsum dolor sit amet', 'consectetur adipisicing elit', 'This is my house', 'quae aperiam solut']; var counter = 0; $('textarea').click(function() { if(array[counter] == string.trim()) { $('.result').text('Matching').css('color', 'green'); } else { $('.result').text('Not matching').css('color', 'red'); } $('textarea').text(array[counter]); counter = (counter + 1) % array.length; });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <textarea readonly placeholder="Click Me" class="textArea"></textarea> <div class="result"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.