
本文旨在解决在使用 jQuery 和 Bootstrap 构建动态表单时,如何正确地从模板克隆元素,并设置输入框的值。我们将详细讲解如何使用 `template.content.cloneNode(true)` 和 `$template.contents().clone()` 来克隆模板内容,并使用 jQuery 的 `val()` 方法设置输入框的值,同时演示如何添加 Bootstrap 5 依赖项以确保代码的正确运行。
在使用 jQuery 和 Bootstrap 构建动态表单时,经常需要从模板中克隆元素,并根据数据动态设置输入框的值。本文将详细介绍如何实现这一功能,并避免一些常见的错误。
在 JavaScript 中,从 <template> 元素克隆内容是动态生成 HTML 的常见方法。有两种主要方式可以实现:
选择哪种方法取决于你的代码风格和需求。如果你的代码主要使用 jQuery,那么第二种方法可能更方便。
示例:
const $container = $('#gridrow-field-container');
const $template = $('#gridrow-template-input-group');
const item = {
MeetingPollingPartsValues: [
{ QuestionValue: 'One' },
{ QuestionValue: 'Two' },
{ QuestionValue: 'Three' }
]
};
$(item.MeetingPollingPartsValues).each((index, partValue) => {
const $inputGroup = $template.contents().clone();
const $inputControl = $inputGroup.find('input');
$inputControl.val(partValue.QuestionValue);
$inputControl.attr('placeholder', `Row ${index + 1}`);
$inputControl.attr('data-value', partValue.QuestionValue);
$container.append($inputGroup);
});在这个例子中,我们使用 $template.contents().clone() 克隆模板内容,然后使用 find('input') 找到输入框,并使用 val() 方法设置其值。
使用 jQuery 的 val() 方法可以方便地设置输入框的值。例如:
const $inputControl = $inputGroup.find('input');
$inputControl.val(partValue.QuestionValue);这会将输入框的值设置为 partValue.QuestionValue。
为了确保代码能够正确运行,需要添加 Bootstrap 5 的依赖项。这包括 CSS 和 JavaScript 文件。
示例:
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous"> <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet"/> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
确保这些依赖项在你的 HTML 文件中正确引入。
下面是一个完整的示例,展示了如何使用 jQuery 和 Bootstrap 动态设置输入框的值:
<!DOCTYPE html>
<html>
<head>
<title>Dynamic Input Groups</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-9ndCyUaIbzAi2FUVXJi0CjmCapSmO7SnpJef0486qhLnuZ2cdeRhO02iuK6FUUVM" crossorigin="anonymous">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js" integrity="sha384-geWF76RCwLtnZ8qwWowPQNguL3RmwHVBC9FhGdlKrxdiJJigb/j/68SIy3Te4Bkz" crossorigin="anonymous"></script>
</head>
<body>
<template id="gridrow-template-input-group">
<div class="row">
<div class="input-group mb-3">
<input type="text" name="name[]" class="form-control"
placeholder="Row x" data-value="0" >
<div class="input-group-text" style="cursor:pointer;" onclick="RemoveRow(this)">
<i class="fa fa-remove" style="color:#CDCDCD"></i>
</div>
</div>
</div>
</template>
<div id="gridrow-field-container" class="container"></div>
<script>
const $container = $('#gridrow-field-container');
const $template = $('#gridrow-template-input-group');
const RemoveRow = (span) => {
$(span).closest('.row').remove();
}
const item = {
MeetingPollingPartsValues: [
{ QuestionValue: 'One' },
{ QuestionValue: 'Two' },
{ QuestionValue: 'Three' }
]
};
$(item.MeetingPollingPartsValues).each((index, partValue) => {
const $inputGroup = $template.contents().clone();
const $inputControl = $inputGroup.find('input');
$inputControl.val(partValue.QuestionValue);
$inputControl.attr('placeholder', `Row ${index + 1}`);
$inputControl.attr('data-value', partValue.QuestionValue);
$container.append($inputGroup);
});
</script>
</body>
</html>通过本文,你应该掌握了如何使用 jQuery 和 Bootstrap 动态设置输入框的值。关键在于正确克隆模板内容,并使用 val() 方法设置输入框的值。同时,确保添加必要的 Bootstrap 依赖项,以确保代码能够正确运行。通过遵循这些步骤,你可以轻松地构建动态表单,并根据数据动态设置输入框的值。
以上就是使用 jQuery 和 Bootstrap 动态设置输入框的值的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号