Home >Web Front-end >JS Tutorial >How to manipulate input elements in jQuery
Manipulating input elements in jQuery is a very common operation. jQuery can easily obtain the value of the input element, set the value, listen for events, etc. The following will introduce in detail how to operate input elements in jQuery, with specific code examples.
To get the value of the input element, you can use jQuery’s val()
method, as shown below:
// 获取id为input1的input元素的值 var value = $('#input1').val(); console.log(value);
To set the value of the input element, you can also use the val()
method, as shown below:
// 设置id为input2的input元素的值为"Hello World" $('#input2').val("Hello World");
You can use the on('input', function(){})
method to listen to the input event of the input element, as shown below:
// 监听id为input3的input元素的输入事件 $('#input3').on('input', function(){ var value = $(this).val(); console.log("输入的值为:" + value); });
You can listen to the event that the input box loses focus through the blur()
method, as shown below:
// 监听id为input4的input元素失去焦点事件 $('#input4').blur(function(){ var value = $(this).val(); console.log("失去焦点,输入的值为:" + value); });
To disable input elements, you can use the prop('disabled', true)
method as follows:
// 禁用id为input5的input元素 $('#input5').prop('disabled', true);
Through the above method, we can easily operate input elements in jQuery, including getting values, setting values, listening to events, etc. In actual development, operations on input elements are very common. Mastering these operation methods can make us more flexible in page interaction and data processing.
The above is a detailed introduction and code examples on how to operate input elements in jQuery. I hope it will be helpful to everyone!
The above is the detailed content of How to manipulate input elements in jQuery. For more information, please follow other related articles on the PHP Chinese website!