What to do if jquery .val() doesn't work

藏色散人
Release: 2022-11-30 10:03:52
Original
2761 people have browsed it

The solution to jquery .val() not working: 1. Open the corresponding js file; 2. Check the method of getting or assigning values ​​to the form in jquery; 3. Modify and use "$(\' Just use the form ID\').val()" method to get the value.

What to do if jquery .val() doesn't work

The operating environment of this tutorial: Windows 7 system, jquery version 3.2.1, Dell G3 computer.

What should I do if jquery .val() doesn’t work?

Problem description:

About using .val() in jquery, the value cannot be obtained?

var account=$("#<%=administrators.getAccount()%>").val();
alert(account);
Copy after login

Use the above .val() method to get the value to be empty;

var account=$("#<%=administrators.getAccount()%>").attr(&#39;id&#39;);
alert(account);
Copy after login

Use the above .attr('id') method to get the value. Why? There is a value attribute in the input tag, and it is also assigned a value.

Problem analysis and solution:

The data cannot be obtained by using .val() here, and must be replaced by .text().

So as the name suggests: the val() method is generally used in the input attribute. To get the value, there must be a value attribute. You can also set the value via xx via val("xx").

To get or assign a value to a form in jquery, we only need a simple $(\'form ID\').val() to get the value. If you assign a value, you can use $(\'form ID\').val(\'content\') can be achieved.

What to do if jquery .val() doesnt work

Extended information

Use text(), html(), and val() methods in jQuery to assign and get values ​​from Html elements

In jQuery, it is very easy to get any Html element using a selector. On the Html page, it is divided into form elements and non-form elements: as for the text() method encapsulated by jQuery, its main function is to give Non-form element assignment and value operations.

The text() method is a jQuery method. The elements operated by this method must be found using the jQuery selector. If the elements are found using JavaScript, the text() method cannot be used. Use div1.text() to get the value of the div element, and use div1.text("new value") to assign the value to the div element.

Note:

Using text() cannot obtain the value of form elements, such as the value of a text box. jQuery's text() method is equivalent to JavaScript's innerText property.

Recommended learning: "jQuery Video Tutorial"

The above is the detailed content of What to do if jquery .val() doesn't work. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template