[英]Change value of input with jQuery
I have multiple input
's which have different data-id
's.我有多个
input
有不同data-id
。
If the input is clicked, I want to change the value of that input.如果单击输入,我想更改该输入的值。 I tried this but it didn't work, I've searched but couldn't find a solution.
我试过这个,但它没有用,我已经搜索但找不到解决方案。
<script>
$('[data-id]').click(function() {
$(this).attr('data-id').parent().value("changed value successfully!");
//I tried this one below as well
//$(this).attr('data-id').parent().input.value("changed value successfully!");
});
</script>
<div>
<input type="submit" data-id="1" value="hello world">
</div>
<div>
<input type="submit" data-id="2" value="hello world">
</div>
<div>
<input type="submit" data-id="3" value="hello world">
</div>
Does anyone know how I can fix this?有谁知道我该如何解决这个问题?
Thanks谢谢
https://jsfiddle.net/j4bu6923/3/ https://jsfiddle.net/j4bu6923/3/
$('input[data-id]').click(function() { $(this).val("changed value successfully;"); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div> <input type="text" data-id="1" value="hello world"> </div> <div> <input type="text" data-id="2" value="hello world"> </div> <div> <input type="text" data-id="3" value="hello world"> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.