У меня есть форма, которая в зависимости от выбранного вами варианта показывает или скрывает div.
В этом div у меня есть два элемента ввода.
Если div настроен на отображение, у меня есть функция, которая добавляет требуемый атрибут true.
Если он не отображается, он не отображается, но говорит мне
Недопустимый элемент управления формы с именем = 'montant_creance' не может быть сфокусирован. index.php:1 Недопустимый элемент управления формы с именем='condition2' не может быть сфокусирован.
Но я действительно не понимаю, почему, потому что div не настроен на отображение.
Вот функция, которую я использую
<script type="text/javascript">
function addattribute()
{
if (document.getElementById('montant').style.display == "block")
{
document.getElementsByName('montant_creance')[1].setAttribute('required');
document.getElementsByName('condition2')[0].setAttribute('required');
}
}
</script>
Итак, может ли кто-нибудь объяснить мне, почему он показывает мне это сообщение в консоли? Я пробовал использовать эту функцию
<script type="text/javascript">
function addattribute()
{
if (document.getElementById('montant').style.display == "block")
{
document.getElementsByName('montant_creance')[0].setAttribute('required');
document.getElementsByName('condition2')[0].setAttribute('required');
}
if (document.getElementById('montant').style.display == "none")
{
document.getElementsByName('montant_creance')[0].setAttribute('required','false');
document.getElementsByName('condition2')[0].setAttribute('required','false');
}
}
</script>
часто говорят
Недопустимый элемент управления формы с именем = 'montant_creance' не может быть сфокусирован. index.php:1 Недопустимый элемент управления формы с именем='condition2' не может быть сфокусирован.
Я также попытался добавить скрытый тип ввода; используя эту функцию:
<script type="text/javascript">
function addattribute()
{
if (document.getElementById('montant').style.display == "block")
{
document.getElementsByName('montant_creance')[1].setAttribute('required');
document.getElementsByName('condition2')[1].setAttribute('required');
}
if (document.getElementById('montant').style.display == "none")
{
document.getElementsByName('montant_creance')[0].setAttribute('required','false');
document.getElementsByName('condition2')[0].setAttribute('required','false');
}
}
</script>
вот предварительный просмотр: http://jsfiddle.net/VUZQP/
Примите все мое глубокое уважение