> For the complete documentation index, see [llms.txt](https://docs.webmix.cc/javascript-web/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.webmix.cc/javascript-web/6.-biao-chan-form/6.2-yan-zheng.md). Agents can also search and read this documentation through its [MCP server](https://docs.webmix.cc/javascript-web/~gitbook/mcp).

# 6.2 設定表單資料

## text 一般文字框的設定值

html：

```html
<input type="text" class="the_text" id="the_text">
```

「設定值」的語法：

```javascript
var the_text = document.getElementById("the_text");

// 以下三種方式皆可
the_text.value = "測試"; // 這個較常見，但不會改變到標籤上的屬性

//the_text.setAttribute("value", "測試二");
//the_text.defaultValue = "測試三"; // 極少見
```

例：<https://codepen.io/editor/carlos411/pen/01a1ceb9-5ee9-70cf-a480-28f12e3906ae>

{% embed url="<https://codepen.io/editor/carlos411/pen/01a1ceb9-5ee9-70cf-a480-28f12e3906ae>" %}

## textarea 多行文字框設定值

html：

```html
<textarea id="the_textarea"></textarea>
<button type="button" id="the_btn">按鈕</button>
```

JS：

如果要斷行，要用 **`\n`**。

```javascript
var the_textarea = document.getElementById("the_textarea");
the_textarea.innerHTML = "這是文字\n新的一行";
```

例：<https://codepen.io/editor/carlos411/pen/01a1cebb-d973-7ce0-a652-d44a7a2b78d1>

{% embed url="<https://codepen.io/editor/carlos411/pen/01a1cebb-d973-7ce0-a652-d44a7a2b78d1>" %}

## select 下拉選單的設定值

html：

```html
<select name="the_select" id="the_select">
  <option value="a">選項一</option>
  <option value="b">選項二</option>
  <option value="c">選項三</option>
</select>
```

JS：

```javascript
var the_select = document.getElementById("the_select");
the_select.value = "c"; // 設定值
```

例：<https://codepen.io/editor/carlos411/pen/01a1cebd-5de7-7d3c-b9af-25c237ef9291>

{% embed url="<https://codepen.io/editor/carlos411/pen/01a1cebd-5de7-7d3c-b9af-25c237ef9291>" %}

## checkbox 的勾選

### 判斷有無勾選

html：

```html
<input type="checkbox" id="the_checkbox">
<button type="button" id="the_btn">取文字</button>
```

JS：

```javascript
var the_checkbox = document.getElementById("the_checkbox");
if(the_checkbox.checked){
  alert("有勾選");
}else{
  alert("沒有勾選");
}
```

例：<https://codepen.io/editor/carlos411/pen/01a1cffb-0d10-7a55-ab81-c8e7e5a40c13>

{% embed url="<https://codepen.io/editor/carlos411/pen/01a1cffb-0d10-7a55-ab81-c8e7e5a40c13>" %}

### 設定勾選或不勾選

JS：

```javascript
var the_checkbox = document.getElementById("the_checkbox");
if(the_checkbox.checked){ // 判斷目前有無勾選
  the_checkbox.checked = false; // 不勾選
}else{
  the_checkbox.checked = true; // 勾選
}
```

例：<https://codepen.io/editor/carlos411/pen/01a1cffc-8c11-745a-990b-f7047c2d5c1c>

{% embed url="<https://codepen.io/editor/carlos411/pen/01a1cffc-8c11-745a-990b-f7047c2d5c1c>" %}

## radio 單選框的選取

### 判斷有無選取

html：

```html
<input type="radio" class="food_type" name="food_type" id="check1" value="1" checked>
<input type="radio" class="food_type" name="food_type" id="check2" value="2">
<input type="radio" class="food_type" name="food_type" id="check3" value="3">

<button type="button" id="the_btn">取文字</button>
```

JS：

```javascript
var food_type = document.querySelectorAll("input[name='food_type']");
food_type.forEach(function(item, i){ // 執行迴圈
  console.log(item.checked); // 該項是否有勾選
});
```

例：<https://codepen.io/editor/carlos411/pen/01a1d000-a4f7-758b-a531-44ec89e0f73b>

{% embed url="<https://codepen.io/editor/carlos411/pen/01a1d000-a4f7-758b-a531-44ec89e0f73b>" %}

### 設定某項選取

JS：

```javascript
var food_type = document.getElementById("check3");
food_type.checked = true;
```

例：<https://codepen.io/editor/carlos411/pen/01a1d002-537e-7c5a-8e0d-cffd1e822e6f>

{% embed url="<https://codepen.io/editor/carlos411/pen/01a1d002-537e-7c5a-8e0d-cffd1e822e6f>" %}
