Получение значения элемента form action ="..."
Здравствуйте! Подскажите, пожалуйста, как можно с помощью JS или jQuery получить значение элемент в атрибуте form action.
Например, у меня есть несколько форм на сайте и у них разные action: <form action="call2/5" method="post" class="bp"> <form action="order2/5" method="post"> <form action="order/5" method="post"> <form action="order/5" method="post"> <form action="order/5" method="post"> <form action="call" method="post"> <form action="feedback" method="post"> Мне необходимо получить: call2/5, order2/5, order/5 и т.д. Хочу записать значение отдельно в переменную. Спасибо! |
karnager,
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
<style type="text/css">
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script>
$(function() {
$(document).on("click", "form", function() {
alert(this.getAttribute("action"));
});
});
</script>
</head>
<body>
<form action="call2/5" method="post" class="bp">test click</form>
<form action="order2/5" method="post">test click</form>
<form action="order/5" method="post">test click</form>
<form action="order/5" method="post">test click</form>
<form action="order/5" method="post">test click</form>
<form action="call" method="post">test click</form>
<form action="feedback" method="post">test click</form>
</body>
</html>
|
karnager,
тоже самое на js.
document.addEventListener("click", function(event) {
var target = event.target.closest("form");
if(target) alert(target.getAttribute("action"))
})
|
Спасибо, если это заключено в функцию, то как сделать для нее замыкание, то есть что нужно вернуть в конструкции после return?
|
karnager,
не понимаю. |
В моем случае реализация должна удовлетворять 2 условиям:
1. Скрипт должен быть обернут в анонимный функциональный блок ( function() { ... }). 2. Функция должна иметь оператор return. Моя задача получать значение form action на всем сайте без каких-либо кликов и взаимодействий с формой, просто список |
karnager,
<!DOCTYPE html>
<html>
<head>
<title>Untitled</title>
<meta charset="utf-8">
<style type="text/css">
</style>
</head>
<body>
<form action="call2/5" method="post" class="bp">test click</form>
<form action="order2/5" method="post">test click</form>
<form action="order/5" method="post">test click</form>
<form action="order/5" method="post">test click</form>
<form action="order/5" method="post">test click</form>
<form action="call" method="post">test click</form>
<form action="feedback" method="post">test click</form>
<script>
document.write(( function(selector) { return [...document.querySelectorAll(selector)].map(form => form.getAttribute("action")) })("form"));
</script>
</body>
</html>
|
Цитата:
<form action="call2/5" method="post" class="bp"></form>
<form action="order2/5" method="post"></form>
<form action="order/5" method="post"></form>
<form action="order/5" method="post"></form>
<form action="order/5" method="post"></form>
<form action="call" method="post"></form>
<form action="feedback" method="post"></form>
<script>
// список атрибутов action всех форм
var actions = Array.from(document.querySelectorAll("form"), form => form.getAttribute("action"));
// например, вывод списка
document.write(actions.join("<br>"));
</script>
Вариант №2. Вычисленное значение атрибута
<form action="call2/5" method="post" class="bp"></form>
<form action="order2/5" method="post"></form>
<form action="order/5" method="post"></form>
<form action="order/5" method="post"></form>
<form action="order/5" method="post"></form>
<form action="call" method="post"></form>
<form action="feedback" method="post"></form>
<script>
// список атрибутов action всех форм
var actions = Array.from(document.querySelectorAll("form"), form => form.action);
// например, вывод списка
document.write(actions.join("<br>"));
</script>
|
| Часовой пояс GMT +3, время: 04:00. |