使表单/div 可编辑

发布于 2024-11-08 02:23:16 字数 967 浏览 6 评论 0原文

您好,我有大约 6 个目前只读的字段。我现在必须使它们可编辑。所以我正在考虑使用一个图像/按钮,单击它会触发编辑功能。因此,单击该按钮时,我将加载一个包含文本框的表单,这使得这些字段可编辑。我正在尝试使用 JQuery 执行此操作,但我无法理解如何从只读字段中获取可编辑表单。

如何加载一个具有与我们之前的字段相同的字段并允许用户编辑的表单?

这是我现在的代码,我必须将可编辑表单加载到 id 中。

<div id="student-infomercial">
<ul>
<label>Name:</label>
<div ><?php echo $name; ?></div>
</li>
<li>
<label>Age:</label>
<div ><?php echo $age; ?></div>
</li>
<li>
<label>Country:</label>
<div ><?php echo $country; ?></div>
</li>
<li>
<label>State:</label>
<div ><?php echo $state; ?></div>
</li>
<li>
<label>City:</label>
<div ><?php echo $city; ?></div>
</li>
<li>
<li>
<label>Zip:</label>
<div ><?php echo $zip; ?></div>
</li>
</ul>
</div>

Hello I have around 6 fields which are read only presently. I now would have to make them editable. So I am thinking of having one image/button, which onclick would trigger the edit functionality. So onclick of that button, I would have a form load up which would have the textboxes, which makes those fields editable. I am trying to do this with JQuery, I am unable to understand on how to get a editable form out of read only fields.

How is it possible to load up a form which would have same fields as the one we had earlier and allows the users to edit?

This is my present code, I would have to load my editable form into the id.

<div id="student-infomercial">
<ul>
<label>Name:</label>
<div ><?php echo $name; ?></div>
</li>
<li>
<label>Age:</label>
<div ><?php echo $age; ?></div>
</li>
<li>
<label>Country:</label>
<div ><?php echo $country; ?></div>
</li>
<li>
<label>State:</label>
<div ><?php echo $state; ?></div>
</li>
<li>
<label>City:</label>
<div ><?php echo $city; ?></div>
</li>
<li>
<li>
<label>Zip:</label>
<div ><?php echo $zip; ?></div>
</li>
</ul>
</div>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你怎么这么可爱啊 2024-11-15 02:23:16

所以我猜你是在问如何将你的元素转换成?你可以做到,但可能会很麻烦。如果我是你,我会改为使用以下 html:

<div id="student-infomercial">
<ul>
<label>Name:</label>
<input class="disabled" disable="true"><?php echo $name; ?></input>
</li>
<li>
<label>Age:</label>
<input class="disabled" disabled="true"><?php echo $age; ?></input>
</li>
</div>

然后在你的 javascript 中:

$("#buttonId").bind("click", function(){
   var fields = $(".disabled");
    fields.removeClass("disabled");
    fields.addClass("enabled");
    fields.removeAttr("disabled", "");
});

如果你坚持使用

那么你将不得不转换

在某些事件上将标签添加到 标签。

还有很多方法可以实现您的目标;以上只是我脑海中浮现的解决方案。

So I guess you're asking how to transform your element into ? You can do it, but it probably will be too much trouble. If I were you I would have following html instead:

<div id="student-infomercial">
<ul>
<label>Name:</label>
<input class="disabled" disable="true"><?php echo $name; ?></input>
</li>
<li>
<label>Age:</label>
<input class="disabled" disabled="true"><?php echo $age; ?></input>
</li>
</div>

then in your javascript:

$("#buttonId").bind("click", function(){
   var fields = $(".disabled");
    fields.removeClass("disabled");
    fields.addClass("enabled");
    fields.removeAttr("disabled", "");
});

If you insist on using <div> then you will have to transform the <div> tags to <input> tags upon some event.

There are many many more ways you can achieve your goal; the above is just the solution that pops into my mind.

扛起拖把扫天下 2024-11-15 02:23:16

开始使用 https://github.com/tectual/jQuery-Editable。它有一个 data-for 属性。

基本上,您可以使用字段的 data-for 属性定义显示 div/标签,然后调用 $('formCssSelector').editables() 将使它们全部可编辑。并设置 onFreeze 函数,您可以定义它以使用您首选的验证系统,如果所有字段均有效,则使用 $.ajax

示例页面提交 jquery 可编辑 html 5 技术

Start using https://github.com/tectual/jQuery-Editable. it has a data-for attribute.

Basically you define the display div/label with data-for attribute for a field and then calling $('formCssSelector').editables() will make them all editable. and setting onFreeze function you can define to use your preferred validation system and if all fields are valid submit using $.ajax

sample page for jquery editable html 5 technique

°如果伤别离去 2024-11-15 02:23:16

您只是问如何取消禁用属性吗?

$("#myform input").attr("disabled", "");

Are you just asking how to unset the disabled attribute?

$("#myform input").attr("disabled", "");
岁月如刀 2024-11-15 02:23:16

您看过 jeditable 吗?我认为它会很好地满足您的需求。

编辑以解决评论。

这是一个起点

$("li").each(function () {
  var text = $(this).text();
  var $input = $("<input>");
  $input.attr("type", "text");
  $input.attr("value", text);
  $(this).html($input);
});

Have you taken a look at jeditable? I think it will work well for what you want.

EDIT to address comment.

Here is a starting point

$("li").each(function () {
  var text = $(this).text();
  var $input = $("<input>");
  $input.attr("type", "text");
  $input.attr("value", text);
  $(this).html($input);
});
甜尕妞 2024-11-15 02:23:16

因此,另一个选择是使用可编辑表单创建一个完全独立的页面。使用如下代码表示 form.php:

  <form action="whatever.php" method="POST">
     <ul><li>
       <label>Name:</label>
       <input type="text" value="<?php echo $name; ?>"/>
       </li>
       <li>
         <label>Age:</label>
         <input type="text" value="<?php echo $age; ?>"/>
       </li>
       etc 
 </form>

然后在显示页面上,您可以添加一个名为 edit 的按钮,并使用 AJAX 加载表单,如下所示:

   $("#editButton").click(function(){
       $("#student-infomercial").load("form.php");
   });

这样整个显示都将替换为表单。然后,您可以在表单上添加取消按钮以返回到显示。

So another option is to create an entirely separate page with the editable form. Say form.php with code like this:

  <form action="whatever.php" method="POST">
     <ul><li>
       <label>Name:</label>
       <input type="text" value="<?php echo $name; ?>"/>
       </li>
       <li>
         <label>Age:</label>
         <input type="text" value="<?php echo $age; ?>"/>
       </li>
       etc 
 </form>

Then on your display page you can add a button named edit and load the form using AJAX like this:

   $("#editButton").click(function(){
       $("#student-infomercial").load("form.php");
   });

This way the entire display is replaced with a form. You can then add a cancel button on the form to go back to the display.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文