Hướng dẫn html select option set selected value php - html select option đặt giá trị đã chọn php

Một cách khác [như được sử dụng trong WordPress] liên kết với mã WordPress sử dụng khả năng tái sử dụng sẽ là:

    
        >Auto
    

Hàm được chọn [] xác định nếu giá trị được chọn và đặt tùy chọn đã chọn.

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  [true] The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected[ $selected, $current = true, $echo = true ] {
    return __checked_selected_helper[ $selected, $current, $echo, 'selected' ];
}

Hàm được chọn [] lần lượt gọi hàm trợ giúp bên dưới để so sánh và xác định xem giá trị có được chọn hay không. Nó trả về 'được chọn = đã chọn' hoặc ''.

/**
 * Private helper function for checked, selected, disabled and readonly.
 *
 * Compares the first two arguments and if identical marks as $type
 *
 * @since 2.8.0
 * @access private
 *
 * @param mixed  $helper  One of the values to compare
 * @param mixed  $current [true] The other value to compare if not just true
 * @param bool   $echo    Whether to echo or just return the string
 * @param string $type    The type of checked|selected|disabled|readonly we are doing
 * @return string html attribute or empty string
 */
function __checked_selected_helper[ $helper, $current, $echo, $type ] {
    if [ [string] $helper === [string] $current ] {
        $result = " $type='$type'";
    } else {
        $result = '';
    }

    if [ $echo ] {
        echo $result;
    }

    return $result;
}

Tóm tắt: Trong hướng dẫn này, bạn sẽ tìm hiểu cách sử dụng phần tử

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  [true] The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected[ $selected, $current = true, $echo = true ] {
    return __checked_selected_helper[ $selected, $current, $echo, 'selected' ];
}
7 để tạo danh sách thả xuống và hộp danh sách và cách lấy các giá trị được chọn từ phần tử
/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  [true] The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected[ $selected, $current = true, $echo = true ] {
    return __checked_selected_helper[ $selected, $current, $echo, 'selected' ];
}
7 trong PHP.
: in this tutorial, you will learn how to use the
/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  [true] The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected[ $selected, $current = true, $echo = true ] {
    return __checked_selected_helper[ $selected, $current, $echo, 'selected' ];
}
7 element to create a drop-down list and a list box and how to get the selected values from the
/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  [true] The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected[ $selected, $current = true, $echo = true ] {
    return __checked_selected_helper[ $selected, $current, $echo, 'selected' ];
}
7 element in PHP.

Giới thiệu nhanh về yếu tố

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  [true] The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected[ $selected, $current = true, $echo = true ] {
    return __checked_selected_helper[ $selected, $current, $echo, 'selected' ];
}
7 là một thành phần HTML cung cấp danh sách các tùy chọn. Điều sau đây cho thấy cách xác định phần tử
/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  [true] The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected[ $selected, $current = true, $echo = true ] {
    return __checked_selected_helper[ $selected, $current, $echo, 'selected' ];
}
7 trong HTML:

Background Color: --- Choose a color --- Red Green Blue

Code language: HTML, XML [xml]

Phần tử

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  [true] The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected[ $selected, $current = true, $echo = true ] {
    return __checked_selected_helper[ $selected, $current, $echo, 'selected' ];
}
7 có hai thuộc tính quan trọng:

  • /**
     * Private helper function for checked, selected, disabled and readonly.
     *
     * Compares the first two arguments and if identical marks as $type
     *
     * @since 2.8.0
     * @access private
     *
     * @param mixed  $helper  One of the values to compare
     * @param mixed  $current [true] The other value to compare if not just true
     * @param bool   $echo    Whether to echo or just return the string
     * @param string $type    The type of checked|selected|disabled|readonly we are doing
     * @return string html attribute or empty string
     */
    function __checked_selected_helper[ $helper, $current, $echo, $type ] {
        if [ [string] $helper === [string] $current ] {
            $result = " $type='$type'";
        } else {
            $result = '';
        }
    
        if [ $echo ] {
            echo $result;
        }
    
        return $result;
    }
    
    2 -
    /**
     * Private helper function for checked, selected, disabled and readonly.
     *
     * Compares the first two arguments and if identical marks as $type
     *
     * @since 2.8.0
     * @access private
     *
     * @param mixed  $helper  One of the values to compare
     * @param mixed  $current [true] The other value to compare if not just true
     * @param bool   $echo    Whether to echo or just return the string
     * @param string $type    The type of checked|selected|disabled|readonly we are doing
     * @return string html attribute or empty string
     */
    function __checked_selected_helper[ $helper, $current, $echo, $type ] {
        if [ [string] $helper === [string] $current ] {
            $result = " $type='$type'";
        } else {
            $result = '';
        }
    
        if [ $echo ] {
            echo $result;
        }
    
        return $result;
    }
    
    2 liên kết phần tử
    /**
     * Outputs the html selected attribute.
     *
     * Compares the first two arguments and if identical marks as selected
     *
     * @since 1.0.0
     *
     * @param mixed $selected One of the values to compare
     * @param mixed $current  [true] The other value to compare if not just true
     * @param bool  $echo     Whether to echo or just return the string
     * @return string html attribute or empty string
     */
    function selected[ $selected, $current = true, $echo = true ] {
        return __checked_selected_helper[ $selected, $current, $echo, 'selected' ];
    }
    
    7 với phần tử
    /**
     * Private helper function for checked, selected, disabled and readonly.
     *
     * Compares the first two arguments and if identical marks as $type
     *
     * @since 2.8.0
     * @access private
     *
     * @param mixed  $helper  One of the values to compare
     * @param mixed  $current [true] The other value to compare if not just true
     * @param bool   $echo    Whether to echo or just return the string
     * @param string $type    The type of checked|selected|disabled|readonly we are doing
     * @return string html attribute or empty string
     */
    function __checked_selected_helper[ $helper, $current, $echo, $type ] {
        if [ [string] $helper === [string] $current ] {
            $result = " $type='$type'";
        } else {
            $result = '';
        }
    
        if [ $echo ] {
            echo $result;
        }
    
        return $result;
    }
    
    5
  • /**
     * Private helper function for checked, selected, disabled and readonly.
     *
     * Compares the first two arguments and if identical marks as $type
     *
     * @since 2.8.0
     * @access private
     *
     * @param mixed  $helper  One of the values to compare
     * @param mixed  $current [true] The other value to compare if not just true
     * @param bool   $echo    Whether to echo or just return the string
     * @param string $type    The type of checked|selected|disabled|readonly we are doing
     * @return string html attribute or empty string
     */
    function __checked_selected_helper[ $helper, $current, $echo, $type ] {
        if [ [string] $helper === [string] $current ] {
            $result = " $type='$type'";
        } else {
            $result = '';
        }
    
        if [ $echo ] {
            echo $result;
        }
    
        return $result;
    }
    
    6 - Thuộc tính
    /**
     * Private helper function for checked, selected, disabled and readonly.
     *
     * Compares the first two arguments and if identical marks as $type
     *
     * @since 2.8.0
     * @access private
     *
     * @param mixed  $helper  One of the values to compare
     * @param mixed  $current [true] The other value to compare if not just true
     * @param bool   $echo    Whether to echo or just return the string
     * @param string $type    The type of checked|selected|disabled|readonly we are doing
     * @return string html attribute or empty string
     */
    function __checked_selected_helper[ $helper, $current, $echo, $type ] {
        if [ [string] $helper === [string] $current ] {
            $result = " $type='$type'";
        } else {
            $result = '';
        }
    
        if [ $echo ] {
            echo $result;
        }
    
        return $result;
    }
    
    6 liên kết với giá trị cho một bản gửi biểu mẫu.

Phần tử

/**
 * Private helper function for checked, selected, disabled and readonly.
 *
 * Compares the first two arguments and if identical marks as $type
 *
 * @since 2.8.0
 * @access private
 *
 * @param mixed  $helper  One of the values to compare
 * @param mixed  $current [true] The other value to compare if not just true
 * @param bool   $echo    Whether to echo or just return the string
 * @param string $type    The type of checked|selected|disabled|readonly we are doing
 * @return string html attribute or empty string
 */
function __checked_selected_helper[ $helper, $current, $echo, $type ] {
    if [ [string] $helper === [string] $current ] {
        $result = " $type='$type'";
    } else {
        $result = '';
    }

    if [ $echo ] {
        echo $result;
    }

    return $result;
}
8 được lồng bên trong phần tử
/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  [true] The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected[ $selected, $current = true, $echo = true ] {
    return __checked_selected_helper[ $selected, $current, $echo, 'selected' ];
}
7 xác định một tùy chọn trong menu. Mỗi tùy chọn có thuộc tính

Background Color: --- Choose a color --- Red Green Blue

Code language: HTML, XML [xml]
0. Dữ liệu lưu trữ thuộc tính

Background Color: --- Choose a color --- Red Green Blue

Code language: HTML, XML [xml]
0 được gửi đến máy chủ khi được chọn.

Nếu một tùy chọn không có thuộc tính

Background Color: --- Choose a color --- Red Green Blue

Code language: HTML, XML [xml]
0, thuộc tính

Background Color: --- Choose a color --- Red Green Blue

Code language: HTML, XML [xml]
0 mặc định cho văn bản bên trong phần tử
/**
 * Private helper function for checked, selected, disabled and readonly.
 *
 * Compares the first two arguments and if identical marks as $type
 *
 * @since 2.8.0
 * @access private
 *
 * @param mixed  $helper  One of the values to compare
 * @param mixed  $current [true] The other value to compare if not just true
 * @param bool   $echo    Whether to echo or just return the string
 * @param string $type    The type of checked|selected|disabled|readonly we are doing
 * @return string html attribute or empty string
 */
function __checked_selected_helper[ $helper, $current, $echo, $type ] {
    if [ [string] $helper === [string] $current ] {
        $result = " $type='$type'";
    } else {
        $result = '';
    }

    if [ $echo ] {
        echo $result;
    }

    return $result;
}
8.

Để chọn tùy chọn khi trang tải lần đầu tiên, bạn có thể thêm thuộc tính

Background Color: --- Choose a color --- Red Green Blue

Code language: HTML, XML [xml]
5 vào phần tử
/**
 * Private helper function for checked, selected, disabled and readonly.
 *
 * Compares the first two arguments and if identical marks as $type
 *
 * @since 2.8.0
 * @access private
 *
 * @param mixed  $helper  One of the values to compare
 * @param mixed  $current [true] The other value to compare if not just true
 * @param bool   $echo    Whether to echo or just return the string
 * @param string $type    The type of checked|selected|disabled|readonly we are doing
 * @return string html attribute or empty string
 */
function __checked_selected_helper[ $helper, $current, $echo, $type ] {
    if [ [string] $helper === [string] $current ] {
        $result = " $type='$type'";
    } else {
        $result = '';
    }

    if [ $echo ] {
        echo $result;
    }

    return $result;
}
8.

Ví dụ sau chọn tùy chọn

Background Color: --- Choose a color --- Red Green Blue

Code language: HTML, XML [xml]
7 khi trang lần đầu tiên tải:

Background Color: --- Choose a color --- Red Green Blue

Code language: HTML, XML [xml]

Nhận giá trị đã chọn từ một phần tử

Chúng tôi sẽ tạo một biểu mẫu sử dụng phần tử

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  [true] The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected[ $selected, $current = true, $echo = true ] {
    return __checked_selected_helper[ $selected, $current, $echo, 'selected' ];
}
7.

Đầu tiên, hãy tạo các thư mục và tệp sau:

├── css | └── style.css ├── inc | ├── footer.php | ├── get.php | ├── header.php | └── post.php └── index.php

Code language: JavaScript [javascript]

Thứ hai, đặt mã sau vào tệp tiêu đề.php:

PHP select option

Code language: HTML, XML [xml]

Thứ ba, đặt mã sau vào tệp footer.php:

Code language: HTML, XML [xml]

Thứ tư, thêm mã sau vào tệp get.php để tạo biểu mẫu có một phần tử

/**
 * Outputs the html selected attribute.
 *
 * Compares the first two arguments and if identical marks as selected
 *
 * @since 1.0.0
 *
 * @param mixed $selected One of the values to compare
 * @param mixed $current  [true] The other value to compare if not just true
 * @param bool  $echo     Whether to echo or just return the string
 * @return string html attribute or empty string
 */
function selected[ $selected, $current = true, $echo = true ] {
    return __checked_selected_helper[ $selected, $current, $echo, 'selected' ];
}
7 bằng nút gửi:

Bài Viết Liên Quan

Chủ Đề