發表文章

目前顯示的是有「jQuery」標籤的文章

jQuery序列化form表單,serialize、serializeArray以及serializeObject的用法

圖片
工作上遇在送出表單前要先與資料庫內容比對,我的作法是透過Ajax將表單序列化資料送至後台進行檢查。 表單序列化的可以有三種方式,serialize、serializeArray以及封裝一個方法serializeObject,這邊拿官網範例並設定checkbox陣列,看看有什麼差異。 <form id="form1"> <select name="single"> <option>Single</option> <option>Single2</option> </select> <br> <select name="multiple" multiple="multiple"> <option selected="selected">Multiple</option> <option>Multiple2</option> <option selected="selected">Multiple3</option> </select> <br> <input type="checkbox" name="check[]" value="check1" id="ch1"> <label for="ch1">check1</label> <input type="checkbox" name="check[]" value="check2" checked="checked" id="ch2"> <label for="ch2"...

jQuery-Radio取值/選取

指定某個 radio button 被選取 (使用 prop ) <input type="radio" name="no" value="1">1<br> <input type="radio" name="no" value="2">2<br> <input type="radio" name="no" value="3">3<br> <button type="button" id="btn">選取2</button> $("#btn").on("click", function() { //方法1:指定value $("input[name='no'][value='2']").prop("checked", true); //方法2:指定位置 $("input[name='no']").eq(1).prop("checked", true); }); 取得 Radio 單選框選中的值 <input type="radio" name='myradio' value="1"> <input type="radio" name='myradio' value="2"> <input type="radio" name='myradio' value="3"> <button>Click</button> $("button").on("click", function(){ ...