之前請教了幾位大哥,得到一段代碼。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<title>JS點擊checkbox傳值到input</title>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport"/>
<script type="text/javascript">
function checkedfun() {
var p = document.getElementById("jsrtxt");
var str = this.parentNode.innerText + ";";
if (this.checked) {
p.value += str;
} else {
p.value = p.value.replace(str,"");
}
}
window.onload = function () {
var arr = document.querySelectorAll("input[name=aaa]");
for (var i = 0; i < arr.length; i++) {
arr[i].onclick = checkedfun;
}
}
</script>
</head>
<body>
<input type="text" id="jsrtxt" class="tip" />
<label><input type="checkbox" name="aaa" value="https://bbs.csdn.net/topics/a1"/>復選1</label>
<label><input type="checkbox" name="aaa" value="https://bbs.csdn.net/topics/a2"/>復選2</label>
<label><input type="checkbox" name="aaa" value="https://bbs.csdn.net/topics/a3"/>復選3</label>
</body>
</html>
作用:點擊多選框,可以將“選項”顯示在input里面;就像“復選1,復選2”這樣。
并且是能按照點擊順序顯示。
如果我需要將“值”顯示在input,比如“a1,a2”這樣的話,應該怎么改咧……?
菜鳥試了幾種寫法,但是沒有能成功。
請教一下吧
uj5u.com熱心網友回復:
把var str=this.parantNode.innerText+”;”;
改成
var str=this.value+”;”;
就可以了
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/194914.html
標籤:JavaScript
上一篇:小圖示的橫向排列如何實作
