jquery val() method


  Translation results:

val

##UK [væl] US [væl]

n. British pressure unit, equal to 10N/M; gram equivalent

jquery val() methodsyntax

Function: val() method returns or sets the value of the selected element. The value of an element is set via the value attribute. This method is mostly used for input elements. If this method does not set parameters, it returns the current value of the selected element.

Syntax: $(selector).val(value)

Parameters:

ParametersDescription
valueOptional. Specifies the new content of the selected element.

#Return value: Return the value of the value attribute of the first matching element.

Syntax: $(selector).val()

Set value syntax: $(selector).val(value )

Parameters:

##ParameterDescription value Set the value of the Value attribute.

Use function to set value syntax: $(selector).val(function(index,oldvalue))

Parameters:

ParameterDescriptionfunction(index,oldvalue) Specifies a function that returns the value to be set. index Optional. Accepts the index position of the selector. oldvalue Optional. Accepts the selector's current Value property.

jquery val() methodexample

<html>
<head>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $("button").click(function(){
    $(":text").val("Hello Kitty");
  });
});
</script>
</head>
<body>
<p>Name: <input type="text" name="user" value="Hello World" /></p>
<button>改变文本域的值</button>
</body>
</html>
Run instance »

Click the "Run instance" button to view the online instance