JQuery Вопросы / Ответы
Q-A
Как сделать метод в JQuery
(function($) { jQuery.fn.doSomething = function() { return this.each(function() { var $this = $(this); $this.click(function(event) { event.preventDefault(); // Your function goes here }); }); }; })(jQuery); // А так можно бкдет потом вызвать метод $(document).ready(function() { $('#div1').doSomething(); $('#div2').doSomething(); });
- A:
$(function() { // Way 1 function doosomething() { //Doo something } // Way 2, equivalent to Way 1 var doosomething = function() { // Doo something } $("div.class").click(doosomething); $("div.secondclass").click(doosomething); });
Выполнение нескольких запросов AJAX через jQuery .post
Отправка данных форм ссылкой с помощью js/jquery
<script> $(document).ready(function(){ $("#submit").click(function(evt){ evt.preventDefault(); $("#form").submit(); }) }) </script> <form id="form"> <a id="submit" href="#">Submit</a> </form>
Отправить POST с ипользованием Ajax
A:
<form name="myform" id="myform" action="" method="POST"> <!-- The Name form field --> <label for="name" id="name_label">zoom</label> <input type="text" name="zoom" id="zoom" size="30" value=""/> <br> </select> <script type="text/javascript"> $(document).ready(function(){ $("#myform").submit( function () { $.post( 'post.php', $(this).serialize(), function(data){ $("#results").html(data) } ); return false; }); }); </script>
Отправить POST с ипользованием Ajax
$('select').change(function(){ var v = $(this).val(); $.ajax({ type: "POST", url: "obr.php", data: "val="+v, success: function(msg){ alert( "Data Saved: " + msg ); } }); });
Модальное окно
$("body").on("click", ".js-user-confirm-modal", function() { var id = $(this).data("id"); var deal = $(this).data("deal"); $.ajax({ method: "GET", url: "<?= $get_user_form?>?userId="+id+"&dealId="+deal, dataType: "html", }).done(function( data ) { $(".js-user-modal-content").html(data); $("#userConfirm").modal("show"); }); });
<h4>Покупатель <a href="javascript:void(0)" class="js-user-confirm-modal"> <span class="glyphicon glyphicon-pencil"></span> </a> </h4>
<div class="modal fade" id="userConfirm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"> <div class="modal-dialog" role="document"> <div class="modal-content js-user-modal-content"> ... </div> </div> </div>