1. 程式人生 > >checkbox:click事件觸發span元素內容改變

checkbox:click事件觸發span元素內容改變

func har doctype put .text log orm name each

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>checkbox</title>
    <script src="jquery.js"></script>
</head>
<body>
<p>我想去<span id="spanId">_____</span></p>
<form action="#" id="form">
    <input type="checkbox" name="city" value="南京">南京
    
<input type="checkbox" name="city" value="北京">北京 <input type="checkbox" name="city" value="紐約">紐約 </form> <script> $(function () { $("input[name=city]").click(function () { var arr = []; $("input[name=city]").each(function
() { if(this.checked){ arr.push(this.value); }else{ var index = arr.indexOf(this.value); if(index != -1){ arr[index] = ""; } } }); $(
"#spanId").text(arr.join(",")); }) }); </script> </body> </html>

checkbox:click事件觸發span元素內容改變