9、操作表單
獲取值
如果我們獲得了一個(gè) <input>節(jié)點(diǎn)的引用,就可以直接調(diào)用 value 獲得對(duì)應(yīng)的用戶輸入值:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n567" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="js">// <input type="text" id="email">
var input = document.getElementById('email');
input.value; // '用戶輸入的值'</pre>
這種方式可以應(yīng)用于text 、 password 、 hidden 以及 select 。但是,對(duì)于單選框和復(fù)選框, value屬性返回的永遠(yuǎn)是HTML預(yù)設(shè)的值,而我們需要獲得的實(shí)際是用戶是否“勾上了”選項(xiàng),所以應(yīng)該用checked 判斷:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n569" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="js">// <label><input type="radio" name="weekday" id="monday" value="1">
Monday</label>
// <label><input type="radio" name="weekday" id="tuesday" value="2">
Tuesday</label>
var mon = document.getElementById('monday');
var tue = document.getElementById('tuesday');
mon.value; // '1'
tue.value; // '2'
mon.checked; // true或者false
tue.checked; // true或者false</pre>
設(shè)置值
設(shè)置值和獲取值類似,對(duì)于 text 、 password 、 hidden 以及 select ,直接設(shè)置 value就可以:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n572" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="js">// <input type="text" id="email">
var input = document.getElementById('email');
input.value = 'test@example.com'; // 文本框的內(nèi)容已更新</pre>
提交表單
通過(guò) <form>元素的 submit()方法提交一個(gè)表單,例如,響應(yīng)一個(gè) button 的click 事件,在JavaScript代碼中提交表單:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n575" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="html">
<form id="test-form">
<input type="text" name="test">
<button type="button" onclick="doSubmitForm()">Submit</button>
</form>
<script>
function doSubmitForm() {
var form = document.getElementById('test-form');
// 可以在此修改form的input...
// 提交form:
form.submit();
}
</script></pre>
這種方式的缺點(diǎn)是擾亂了瀏覽器對(duì)form的正常提交。瀏覽器默認(rèn)點(diǎn)擊 <button type="submit"> 時(shí)提交表單,或者用戶在最后一個(gè)輸入框按回車鍵。因此,第二種方式是響應(yīng) <form>本身的onsubmit 事件,在提交form時(shí)作修改:
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n577" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="html">
<form id="test-form" onsubmit="return checkForm()">
<input type="text" name="test">
<button type="submit">Submit</button>
</form>
<script>
function checkForm() {
var form = document.getElementById('test-form');
// 可以在此修改form的input...
// 繼續(xù)下一步:
return true;
}
</script></pre>
注意要return true來(lái)告訴瀏覽器繼續(xù)提交,如果return false ,瀏覽器將不會(huì)繼續(xù)提交form,這種情況通常對(duì)應(yīng)用戶輸入有誤,提示用戶錯(cuò)誤信息后終止提交form。
加Md5
提交隱藏域的密碼
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n581" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="html">
<form id="test-form" onsubmit="return checkForm()">
<input type="text" name="test">
<input type="password" name="input_password">
<input type="indden" id="md5_psd" name="password">
<button type="submit">Submit</button>
</form>
<script>
function checkForm() {
var form = document.getElementById('test-form');
// 可以在此修改form的input...
// 繼續(xù)下一步:
//可以校驗(yàn)表單內(nèi)容
return true;
}
</script></pre>
10、jQuery
JavaScript和JQuery庫(kù)的關(guān)系
JQuery庫(kù),里面存在大量的函數(shù)
獲取jquery
cdn jquery :
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n588" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="html"><script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script></pre>
官網(wǎng)下載:https://jquery.com/download/
公式
$(selector).action();
Jquery選擇器
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n593" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="html"> //原生js,選擇器少,麻煩不好記
? 標(biāo)簽選擇器
? document.getElementsByTagName();
? id選擇器
? document.getElementsById();
? 類選擇器
? document.getElementsByClassName();
? //jQuery
? $('p').click() ;//標(biāo)簽選擇器
? $('#id1').click(); //id選擇器
? $('.class1').click(); //類選擇器 </pre>
文檔工作站:https://jquery.cuishifeng.cn/
Jquery事件
鼠標(biāo)事件,鍵盤事件,其他事件
JQuery操作DOM
節(jié)點(diǎn)文本操作
$('#test-ul li[name=python]').text();//獲得值
$('#test-ul li[name=python]').text('123');//設(shè)置值
$('#test-ul').html();//獲得值
$('#test-ul').html('<strong>123<strong>');//設(shè)置值
CSS操作
$('#test-ul li[name=python]').css("color","red");
元素的顯示和隱藏 本質(zhì)display = none;
<pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" cid="n606" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: var(--monospace); font-size: 0.9em; display: block; break-inside: avoid; text-align: left; white-space: normal; background-image: inherit; background-position: inherit; background-size: inherit; background-repeat: inherit; background-attachment: inherit; background-origin: inherit; background-clip: inherit; background-color: rgb(248, 248, 248); position: relative !important; border: 1px solid rgb(231, 234, 237); border-radius: 3px; padding: 8px 4px 6px; margin-bottom: 15px; margin-top: 15px; width: inherit; color: rgb(51, 51, 51); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;" lang="html">$('#test-ul li[name=python]').show();
$('#test-ul li[name=python]').hide();</pre>
Layer 彈窗組件
Element-ui