Tôi có một nhóm các tùy chọn như thế này:
First
Second
Third
Fourth
Tôi muốn kiểm tra xem một biến
value="1" class="dr">Second
value="3" class="dr">Fourth
9 được xác định. Nếu có, thì tôi muốn thay đổi tùy chọn được chọn trong đó
value="1" class="dr">Second
value="3" class="dr">Fourth
0 bằng
value="1" class="dr">Second
value="3" class="dr">Fourth
1. Một cái gì đó như thế này
value="1" class="dr">Second
value="3" class="dr">Fourth
2Ví dụ.
value="1" class="dr">Second
value="3" class="dr">Fourth
3; Vì vậy, tùy chọn được chọn phải đứng thứ tư. Nếu
value="1" class="dr">Second
value="3" class="dr">Fourth
1 trống hoặc không được xác định, thì đầu tiên phải là tùy chọn được chọn.
rtruszk
3,89413 Huy hiệu vàng35 Huy hiệu bạc53 Huy hiệu Đồng13 gold badges35 silver badges53 bronze badges
Đã hỏi ngày 1 tháng 11 năm 2011 lúc 22:52Nov 1, 2011 at 22:52
Một chiều:
value="1" class="dr">Second
value="3" class="dr">Fourth
Another:
value="1" class="dr">Second
value="3" class="dr">Fourth
Đã trả lời ngày 1 tháng 11 năm 2011 lúc 22:59Nov 1, 2011 at 22:59
AlecalecAlec
8.8828 Huy hiệu vàng38 Huy hiệu bạc43 Huy hiệu đồng8 gold badges38 silver badges43 bronze badges
1
First
Second
Third
Fourth
var test = "";
if [test != '' && parseInt[test]] {
document.getElementById['selectBoxId'].selectedIndex = test;
}
Đã trả lời ngày 1 tháng 11 năm 2011 lúc 22:57Nov 1, 2011 at 22:57
Có Barryyes BarryYes Barry
9.3614 Huy hiệu vàng47 Huy hiệu bạc67 Huy hiệu Đồng4 gold badges47 silver badges67 bronze badges
2
Hủy bỏ "[gõ]" khỏi tên chọn, làm cho nó đơn giản đến "mục". Sau đó thực thi mã này.
value="1" class="dr">Second
value="3" class="dr">Fourth
5Đã trả lời ngày 1 tháng 11 năm 2011 lúc 22:56Nov 1, 2011 at 22:56
// assuming you are using a loop: in the loop where you create the options
$selected_html = $test == $loop_var ? ' selected="selected" ' : '';
echo "$text";
Đã trả lời ngày 1 tháng 11 năm 2011 lúc 23:23Nov 1, 2011 at 23:23
Dean Marshalldean MarshallDean Marshall
1.8251 Huy hiệu vàng11 Huy hiệu bạc10 Huy hiệu đồng1 gold badge11 silver badges10 bronze badges
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ử
6 để 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ử
value="1" class="dr">Second
value="3" class="dr">Fourth
6 trong PHP.: in this tutorial, you will learn how to use the
value="1" class="dr">Second
value="3" class="dr">Fourth
value="1" class="dr">Second
value="3" class="dr">Fourth
6 element to create a drop-down list and a list box and how to get the selected values from the
value="1" class="dr">Second
value="3" class="dr">Fourth
6 element in PHP.Giới thiệu nhanh về yếu tố
value="1" class="dr">Second
value="3" class="dr">Fourth
6 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ử
value="1" class="dr">Second
value="3" class="dr">Fourth
6 trong HTML:Code language: HTML, XML [xml]
Background Color: --- Choose a color --- Red Green Blue
Phần tử
value="1" class="dr">Second
value="3" class="dr">Fourth
6 có hai thuộc tính quan trọng:
1 -First Second Third Fourth var test = ""; if [test != '' && parseInt[test]] { document.getElementById['selectBoxId'].selectedIndex = test; }
1 liên kết phần tửFirst Second Third Fourth var test = ""; if [test != '' && parseInt[test]] { document.getElementById['selectBoxId'].selectedIndex = test; }
6 với phần tửvalue="1" class="dr">Second value="3" class="dr">Fourth
4First Second Third Fourth var test = ""; if [test != '' && parseInt[test]] { document.getElementById['selectBoxId'].selectedIndex = test; }
5 - Thuộc tínhFirst Second Third Fourth var test = ""; if [test != '' && parseInt[test]] { document.getElementById['selectBoxId'].selectedIndex = test; }
5 liên kết với giá trị cho một bản gửi biểu mẫu.First Second Third Fourth var test = ""; if [test != '' && parseInt[test]] { document.getElementById['selectBoxId'].selectedIndex = test; }
Phần tử
First
Second
Third
Fourth
var test = "";
if [test != '' && parseInt[test]] {
document.getElementById['selectBoxId'].selectedIndex = test;
}
7 được lồng bên trong phần tử
value="1" class="dr">Second
value="3" class="dr">Fourth
6 xác định một tùy chọn trong menu. Mỗi tùy chọn có thuộc tính
value="1" class="dr">Second
value="3" class="dr">Fourth
0. Dữ liệu lưu trữ thuộc tính
value="1" class="dr">Second
value="3" class="dr">Fourth
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
value="1" class="dr">Second
value="3" class="dr">Fourth
0, thuộc tính
value="1" class="dr">Second
value="3" class="dr">Fourth
0 mặc định cho văn bản bên trong phần tử
First
Second
Third
Fourth
var test = "";
if [test != '' && parseInt[test]] {
document.getElementById['selectBoxId'].selectedIndex = test;
}
7.Để 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
// assuming you are using a loop: in the loop where you create the options
$selected_html = $test == $loop_var ? ' selected="selected" ' : '';
echo "$text";
4 vào phần tử
First
Second
Third
Fourth
var test = "";
if [test != '' && parseInt[test]] {
document.getElementById['selectBoxId'].selectedIndex = test;
}
7.Ví dụ sau chọn tùy chọn
// assuming you are using a loop: in the loop where you create the options
$selected_html = $test == $loop_var ? ' selected="selected" ' : '';
echo "$text";
6 khi trang lần đầu tiên tải:Code language: HTML, XML [xml]
Background Color: --- Choose a color --- Red Green Blue
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ử
value="1" class="dr">Second
value="3" class="dr">Fourth
6.Đầu tiên, hãy tạo các thư mục và tệp sau:
Code language: JavaScript [javascript]
├── css | └── style.css ├── inc | ├── footer.php | ├── get.php | ├── header.php | └── post.php └── index.php
Thứ hai, đặt mã sau vào tệp tiêu đề.php:
Code language: HTML, XML [xml]
PHP select option
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ử
value="1" class="dr">Second
value="3" class="dr">Fourth
6 bằng nút gửi:
value="1" class="dr">Second
value="3" class="dr">Fourth
0Biểu mẫu sử dụng phương thức
// assuming you are using a loop: in the loop where you create the options
$selected_html = $test == $loop_var ? ' selected="selected" ' : '';
echo "$text";
9 để gửi dữ liệu cho máy chủ web.Cuối cùng, thêm mã sau vào tệp Post.php:
value="1" class="dr">Second
value="3" class="dr">Fourth
1Để có được giá trị được chọn của phần tử
value="1" class="dr">Second
value="3" class="dr">Fourth
6, bạn sẽ sử dụng biến siêu global 1 nếu phương thức biểu mẫu làCode language: HTML, XML [xml]
Background Color: --- Choose a color --- Red Green Blue
// assuming you are using a loop: in the loop where you create the options
$selected_html = $test == $loop_var ? ' selected="selected" ' : '';
echo "$text";
9 và 3 nếu phương thức biểu mẫu làCode language: HTML, XML [xml]
Background Color: --- Choose a color --- Red Green Blue
4.Code language: HTML, XML [xml]
Background Color: --- Choose a color --- Red Green Blue
Ngoài ra, bạn có thể sử dụng hàm Filter_Input [] để vệ sinh giá trị đã chọn.
Nếu bạn chọn tùy chọn đầu tiên của phần tử
value="1" class="dr">Second
value="3" class="dr">Fourth
6, giá trị đã chọn sẽ trống. Mặt khác, giá trị được chọn là màu đỏ, xanh lá cây hoặc xanh dương.Chọn với nhiều tùy chọn
Để bật nhiều lựa chọn, bạn thêm thuộc tính
6 vào phần tửCode language: HTML, XML [xml]
Background Color: --- Choose a color --- Red Green Blue
value="1" class="dr">Second
value="3" class="dr">Fourth
6:
value="1" class="dr">Second
value="3" class="dr">Fourth
2Khi bạn chọn nhiều tùy chọn của phần tử
value="1" class="dr">Second
value="3" class="dr">Fourth
6 và gửi biểu mẫu, tên sẽ chứa nhiều giá trị thay vì một giá trị duy nhất. Để nhận được nhiều giá trị đã chọn, bạn thêm dấu ngoặc vuông [9 sau tên của phần tửCode language: HTML, XML [xml]
Background Color: --- Choose a color --- Red Green Blue
value="1" class="dr">Second
value="3" class="dr">Fourth
6.Hãy cùng xem một ví dụ về việc sử dụng phần tử
value="1" class="dr">Second
value="3" class="dr">Fourth
6 với nhiều lựa chọn.Đầu tiên, hãy tạo các thư mục và tệp sau:
value="1" class="dr">Second
value="3" class="dr">Fourth
3Thứ hai, đặt mã sau vào tệp tiêu đề.php:
value="1" class="dr">Second
value="3" class="dr">Fourth
4Thứ ba, thêm mã sau vào tệp footer.php:
Code language: HTML, XML [xml]
Thứ tư, bao gồm các tệp
2 vàCode language: HTML, XML [xml]
Background Color: --- Choose a color --- Red Green Blue
3 trongCode language: HTML, XML [xml]
Background Color: --- Choose a color --- Red Green Blue
4:Code language: HTML, XML [xml]
Background Color: --- Choose a color --- Red Green Blue
value="1" class="dr">Second
value="3" class="dr">Fourth
6Nếu yêu cầu HTTP là GET, tệp index.php sẽ hiển thị một biểu mẫu từ tệp get.php. Khi biểu mẫu được gửi, tệp Post.php sẽ xử lý việc gửi biểu mẫu.
Thứ năm, tạo một biểu mẫu chứa phần tử
value="1" class="dr">Second
value="3" class="dr">Fourth
6 với thuộc tính 6 trong tệpCode language: HTML, XML [xml]
Background Color: --- Choose a color --- Red Green Blue
7. Tên của phần tửCode language: HTML, XML [xml]
Background Color: --- Choose a color --- Red Green Blue
value="1" class="dr">Second
value="3" class="dr">Fourth
6 có khung hình vuông mở và đóng 9 để PHP có thể tạo một mảng giữ các giá trị chọn.Code language: HTML, XML [xml]
Background Color: --- Choose a color --- Red Green Blue
value="1" class="dr">Second
value="3" class="dr">Fourth
7Cuối cùng, xử lý việc gửi biểu mẫu trong tệp
0:Code language: JavaScript [javascript]
├── css | └── style.css ├── inc | ├── footer.php | ├── get.php | ├── header.php | └── post.php └── index.php
value="1" class="dr">Second
value="3" class="dr">Fourth
8Tệp Post.php sử dụng hàm
1 để lấy các màu được chọn dưới dạng mảng. Nếu bạn chọn một hoặc nhiều màu, tệp Post.php sẽ hiển thị chúng.Code language: JavaScript [javascript]
├── css | └── style.css ├── inc | ├── footer.php | ├── get.php | ├── header.php | └── post.php └── index.php
Bản tóm tắt
- Sử dụng phần tử
6 để tạo danh sách thả xuống.value="1" class="dr">Second value="3" class="dr">Fourth
- Sử dụng thuộc tính
6 để tạo danh sách cho phép nhiều lựa chọn.
Code language: HTML, XML [xml]Background Color: --- Choose a color --- Red Green Blue
- Sử dụng
1 để lấy giá trị đã chọn của phần tử chọn nếu phương thức biểu mẫu là
Code language: HTML, XML [xml]Background Color: --- Choose a color --- Red Green Blue
9 [hoặc// assuming you are using a loop: in the loop where you create the options $selected_html = $test == $loop_var ? ' selected="selected" ' : ''; echo "$text";
3 nếu phương thức biểu mẫu là
Code language: HTML, XML [xml]Background Color: --- Choose a color --- Red Green Blue
4].
Code language: HTML, XML [xml]Background Color: --- Choose a color --- Red Green Blue
- Thêm dấu ngoặc vuông [________ 69] sau tên của phần tử
6 để nhận nhiều giá trị được chọn.value="1" class="dr">Second value="3" class="dr">Fourth
Bạn có thấy hướng dẫn này hữu ích không?