Latest web development tutorials

jQuery Mobile 表單輸入

jQuery Mobile 文本輸入框

輸入字段是通過標準的HTML 元素編碼的,jQuery Mobile 將為它們添加樣式使其看起來更具吸引力,在移動設備上更易使用。 您也能使用新的HTML5 的<input> 類型:

實例

<form method="post" action="demo_form.php">
<div class="ui-field-contain">
<label for="fullname">全名:</label>
<input type="text" name="fullname" id="fullname">

<label for="bday">生日:</label>
<input type="date" name="bday" id="bday">

<label for="email">E-mail:</label>
<input type="email" name="email" id="email" placeholder="你的电子邮箱..">
</div>
</form>

嘗試一下»

提示:請使用placeholder來指定一個簡短的描述,用來描述輸入字段的期望值:

<input placeholder="sometext">

文本域

對於多行文本輸入可使用<textarea> 。

注意:當您鍵入一些文本時,文本域會自動調整大小以適應新增加的行。

實例

<form method="post" action="demo_form.php">
<div class="ui-field-contain">
<label for="info">附加信息:</label>
<textarea name="addinfo" id="info"></textarea>
</div>
</form>

嘗試一下»


搜索輸入框

type="search" 類型的輸入框是在HTML5 中新增的,它是為輸入搜索定義文本字段:

實例

<form method="post" action="demo_form.php">
<div class="ui-field-contain">
<label for="search">搜索:</label>
<input type="search" name="search" id="search">
</div>
</form>

嘗試一下»


單選按鈕

當用戶在有限數量的選擇中僅選取一個選項時,使用單選按鈕。

為了創建一系列單選按鈕,請添加帶有type="radio" 的input 以及相應的label。 把單選按鈕包圍在<fieldset> 元素內。 您也可以添加一個<legend> 元素來定義<fieldset> 的標題。

提示:請使用data-role="controlgroup"來把按鈕組合在一起:

實例

<form method="post" action="demo_form.php">
<fieldset data-role="controlgroup">
<legend>Choose your gender:</legend>
<label for="male">Male</label>
<input type="radio" name="gender" id="male" value="male">
<label for="female">Female</label>
<input type="radio" name="gender" id="female" value="female">
</fieldset>
</form>

嘗試一下»


複選框

當用戶在有限數量的選擇中選取一個或多個選項時,使用複選框:

實例

<form method="post" action="demo_form.php">
<fieldset data-role="controlgroup">
<legend>Choose as many favorite colors as you'd like:</legend>
<label for="red">Red</label>
<input type="checkbox" name="favcolor" id="red" value="red">
<label for="green">Green</label>
<input type="checkbox" name="favcolor" id="green" value="green">
<label for="blue">Blue</label>
<input type="checkbox" name="favcolor" id="blue" value="blue">
</fieldset>
</form>

嘗試一下»


實例

更多實例

如需水平組合單選按鈕或複選框,請使用data-type="horizo​​ntal":

實例

<fieldset data-role="controlgroup" data-type="horizontal">

嘗試一下»

您也可以用一個field 容器包圍<fieldset>:

實例

<div class="ui-field-contain">
<fieldset data-role="controlgroup">
<legend>请选择您的性别:</legend>
</fieldset>
</div>

嘗試一下»

如果您想要您的按鈕中的一個預先選中,請使用HTML 中<input> 的checked 屬性:

實例

<input type="radio" checked>
<input type="checkbox" checked>

嘗試一下»

你可以將表單放在彈窗中:

實例

<a href="#myPopup" data-rel="popup" class="ui-btn ui-btn-inline">Show Popup Form</a>

<div data-role="popup" id="myPopup" class="ui-content">
<form method="post" action="demoform.php">
<div>
<h3>登錄信息</h3>
<label for="usrnm" class="ui-hidden-accessible">用戶名:</label>
<input type="text" name="user" id="usrnm" placeholder="用戶名">
<label for="pswd" class="ui-hidden-accessible">密碼:</label>
<input type="password" name="passw" id="pswd" placeholder="密碼">
</div>
</form>
</div>

嘗試一下»