jQuery+Bootstrap动态修改输入框内容技巧

时间:2025-10-29

浏览:300

作者:佚名

golang学习网今天将给大家带来《jQuery+Bootstrap动态修改输入框内容方法》,感兴趣的朋友请继续看下去吧!以下内容将会涉及到等等知识点,如果你是正在学习文章或者已经是大佬级别了,都非常欢迎也希望大家都能给我建议评论哈~希望能帮助到大家!

使用 jQuery 和 Bootstrap 动态设置输入框的值

本文旨在解决在使用 jQuery 和 Bootstrap 构建动态表单时,如何正确地从模板克隆元素,并设置输入框的值。我们将详细讲解如何使用 `template.content.cloneNode(true)` 和 `$template.contents().clone()` 来克隆模板内容,并使用 jQuery 的 `val()` 方法设置输入框的值,同时演示如何添加 Bootstrap 5 依赖项以确保代码的正确运行。

在使用 jQuery 和 Bootstrap 构建动态表单时,经常需要从模板中克隆元素,并根据数据动态设置输入框的值。本文将详细介绍如何实现这一功能,并避免一些常见的错误。

克隆模板内容

在 JavaScript 中,从 <template> 元素克隆内容是动态生成 HTML 的常见方法。有两种主要方式可以实现:

  1. template.content.cloneNode(true): 这种方法适用于直接操作 DOM 节点,它会克隆模板的内容,包括所有的子节点和属性。
  2. $template.contents().clone(): 这种方法使用 jQuery,它会克隆模板的内容,并将其包装在 jQuery 对象中,方便后续操作。

选择哪种方法取决于你的代码风格和需求。如果你的代码主要使用 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 依赖项

为了确保代码能够正确运行,需要添加 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 兼容。
  • 使用开发者工具检查元素是否正确添加到 DOM 中,以及输入框的值是否正确设置。
  • 避免使用过时的 jQuery 方法,尽量使用现代的 jQuery API。

总结

通过本文,你应该掌握了如何使用 jQuery 和 Bootstrap 动态设置输入框的值。关键在于正确克隆模板内容,并使用 val() 方法设置输入框的值。同时,确保添加必要的 Bootstrap 依赖项,以确保代码能够正确运行。通过遵循这些步骤,你可以轻松地构建动态表单,并根据数据动态设置输入框的值。

到这里,我们也就讲完了《jQuery+Bootstrap动态修改输入框内容技巧》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

本文内容来源于互联网,如有侵权请联系删除。

相关文章

更多

热门专题

更多
首页