Javascript-форум (https://javascript.ru/forum/)
-   Общие вопросы Javascript (https://javascript.ru/forum/misc/)
-   -   Сохранить содержимое блока html с чекбоксами. (https://javascript.ru/forum/misc/85974-sokhranit-soderzhimoe-bloka-html-s-chekboksami.html)

savsoft 06.07.2024 19:49

Сохранить содержимое блока html с чекбоксами.
 
Все привет,

Для одной задачи решил не заморачиваться, а просто сохранить содержимое блока, как html.

Все работало, пока не попались чекбоксы. И вот их состояние нужно сохранять отдельно, или править html, добавлять нужным чекбоксам checked. Может можно как-нибудь по другому?

Вот пример
<!DOCTYPE html> 

<html> 

<head> 
	<title>Test</title> 
	<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
</head> 

<body> 
	<section id="sct">
		<div>
			<input style="background: #80eda5; border: solid 1px green; border-radius: 50%;" type="checkbox" id="tf_t1" value="1">
			<input style="background: #fa9996; border: solid 1px red; border-radius: 50%;" type="checkbox" id="tf_f1" value="1"><br>
			<button id="getsct">Get section html</button>
		</div>
	</section>

<script>
$(function() {
	$("#getsct").click(function() {
		alert($("#sct").html());
	});
});	
</script>

</body> 

</html>

рони 06.07.2024 21:55

savsoft,
<!DOCTYPE html>
<html>

<head>
    <title>Test</title>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
</head>

<body>
    <section id="sct">
        <div>
            <input style="background: #80eda5; border: solid 1px green; border-radius: 50%;" type="checkbox" id="tf_t1" value="1">
            <input style="background: #fa9996; border: solid 1px red; border-radius: 50%;" type="checkbox" id="tf_f1" value="1"><br>
            <button id="getsct">Get section html</button>
        </div>
    </section>
    <script>
        $(function() {
            $("#getsct").click(function() {
                $("#sct :checkbox").each(function(i, e) {
                    e.checked ? e.setAttribute("checked", "checked") : e.removeAttribute("checked");
                });
                alert($("#sct").html());
            });
        });
    </script>
</body>

</html>

savsoft 06.07.2024 22:14

Цитата:

Сообщение от рони (Сообщение 555567)
savsoft,
$("#sct :checkbox").each(function(i, e) {
                    e.checked ? e.setAttribute("checked", "checked") : e.removeAttribute("checked");
                });

Спасибо


Часовой пояс GMT +3, время: 23:46.