📜  jQWidgets jqxRating val() 方法(1)

📅  最后修改于: 2023-12-03 15:16:56.237000             🧑  作者: Mango

jQWidgets jqxRating val() 方法

The val() method in jQWidgets jqxRating is used to get or set the value of the jqxRating widget programmatically. The value represents the rating given by the user.

Syntax

val(value?: number): number
  • value (optional): The value to be set for the jqxRating widget. It should be a number between 0 and the maximum rating value.

Parameters

  • value (optional): The value to be set for the jqxRating widget. It should be a number between 0 and the maximum rating value. If no value is specified, the current value of the jqxRating widget will be returned.

Return Value

The method returns the current value of the jqxRating widget if no value parameter is provided. If a value parameter is provided, the method returns the jqxRating widget instance for chaining multiple method calls.

Usage

Get the current value of the jqxRating widget
var currentValue = $("#myRating").jqxRating("val");

In this example, the val() method is called on the #myRating jqxRating widget element without any parameter. It retrieves the current rating value and assigns it to the currentValue variable.

Set the value of the jqxRating widget
$("#myRating").jqxRating("val", 3);

In this example, the val() method is called on the #myRating jqxRating widget element with a value of 3. It sets the rating value to 3 programmatically.

Example

<div id="myRating"></div>
$("#myRating").jqxRating({
    width: 350,
    height: 35,
    precision: 0.5
});

// Set rating value programmatically
$("#myRating").jqxRating("val", 4.5);

// Get current rating value
var currentValue = $("#myRating").jqxRating("val");
console.log(currentValue); // Output: 4.5

In this example, a jqxRating widget is created with a width of 350 pixels and a height of 35 pixels. The precision property is set to 0.5, allowing half-star ratings.

The val() method is used to set the initial rating value to 4.5 programmatically. Then, the val() method is called again without any parameter to retrieve the current rating value, which is 4.5. The value is logged to the console for demonstration purposes.

Note: The jQWidgets jqxRating widget must be properly initialized before using the val() method.