To dynamically add text after the static “Thank you” text depending on which checkbox is checked after the submit button is clicked, you can use jQuery or plain JavaScript. Here’s a solution using jQuery:
javascript$(document).ready(function() {
$("#submit-form").click(function(e) {
e.preventDefault(); // Prevent form submission
var selectedText = $("input[type='checkbox']:checked + label").text();
$(".success").text("Thank you for " + selectedText).show();
});
});
In this code, we first prevent the default form submission behavior using e.preventDefault();
. Then, we use the :checked
selector to find the checked checkbox and use the adjacent sibling selector +
to select its corresponding label. We extract the text of the selected label and concatenate it with “Thank you for ” before setting it as the text of the
مقالات ذات صلة
element with the class
success
and displaying it using show()
.
For a pure JavaScript solution:
javascriptdocument.getElementById("submit-form").addEventListener("click", function(e) {
e.preventDefault(); // Prevent form submission
var selectedText = "";
var checkboxes = document.querySelectorAll("input[type='checkbox']");
checkboxes.forEach(function(checkbox) {
if (checkbox.checked) {
selectedText = checkbox.nextElementSibling.textContent;
}
});
document.querySelector(".success").textContent = "Thank you for " + selectedText;
document.querySelector(".success").style.display = "block";
});
In this code, we again prevent the default form submission behavior using e.preventDefault();
. We iterate over all checkboxes and check which one is checked. We then set the text content of the
element with the class
success
to “Thank you for ” + selectedText and display it by setting its display
style to "block"
.
المزيد من المعلومات
بالطبع، إذا كنت ترغب في المزيد من المعلومات حول كيفية إضافة نص إلى نص إذا تم تحقيق شرط معين باستخدام jQuery أو JavaScript العادي، يمكنني توضيح ذلك بشكل أكبر. في السياق الحالي، قد يكون من الضروري معرفة المزيد حول كيفية ترتيب عناصر الصفحة وبنية العناصر الأخرى لتحقيق السلوك المرغوب. إذا كانت هناك أي تفاصيل إضافية ترغب في معرفتها أو تغييرات تود تطبيقها على الشيفرة المعروضة، فلا تتردد في طلب ذلك.