Вход

Просмотр полной версии : Получение значения элемента form action ="..."


karnager
07.09.2019, 18:54
Здравствуйте! Подскажите, пожалуйста, как можно с помощью 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 и т.д. Хочу записать значение отдельно в переменную. Спасибо!

рони
07.09.2019, 19:10
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>

рони
07.09.2019, 19:39
karnager,
тоже самое на js.
document.addEventListener("click", function(event) {
var target = event.target.closest("form");
if(target) alert(target.getAttribute("action"))
})

karnager
07.09.2019, 20:15
Спасибо, если это заключено в функцию, то как сделать для нее замыкание, то есть что нужно вернуть в конструкции после return?

рони
07.09.2019, 20:51
karnager,
не понимаю.

karnager
07.09.2019, 21:17
В моем случае реализация должна удовлетворять 2 условиям:

1. Скрипт должен быть обернут в анонимный функциональный блок ( function() { ... }).

2. Функция должна иметь оператор return.

Моя задача получать значение form action на всем сайте без каких-либо кликов и взаимодействий с формой, просто список

рони
07.09.2019, 22:06
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>

Malleys
08.09.2019, 00:06
Мне необходимо получить: call2/5, order2/5, order/5 и т.д. Хочу записать значение отдельно в переменную... просто список

Вариант №1. Значение атрибута <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>