Javascript 是否有一种默认方法可以将编辑按钮添加到引导表单中的每个输入文本字段

Javascript 是否有一种默认方法可以将编辑按钮添加到引导表单中的每个输入文本字段,javascript,forms,twitter-bootstrap,Javascript,Forms,Twitter Bootstrap,我正在使用bootstrap创建一个数据更新表单,在这个表单中,我已经有了数据,用户可以选择编辑任何字段并保存它。由于我不希望用户意外更改字段,因此我最初将输入字段置于禁用状态,并使用与每个文本输入字段关联的编辑按钮 下面是我正在使用的HTML <form role="form"> <div class="form-group"> <label for="name">Name</label> <div class="input-gr

我正在使用bootstrap创建一个数据更新表单,在这个表单中,我已经有了数据,用户可以选择编辑任何字段并保存它。由于我不希望用户意外更改字段,因此我最初将输入字段置于禁用状态,并使用与每个文本输入字段关联的编辑按钮

下面是我正在使用的HTML

<form role="form">
 <div class="form-group">
  <label for="name">Name</label>
  <div class="input-group">
  <input type="text" class="form-control" id="name" 
     value="Skipper" readonly>
  <div class="input-group-addon">
      <span class="glyphicon glyphicon-pencil" onclick=editName()></span>
  </div>
  </div>   
 </div>
 <button type="submit" class="btn btn-default">Submit</button>
</form>

问题是我的表单中至少有15-20个字段,而为每个字段编写javascript函数看起来并不正确。此外,还有一些我正在寻找的功能,以增强用户体验,比如在启用输入字段时隐藏铅笔图标。你们能推荐一种默认的或内置的处理这种表单的方法吗。

为什么不向javascript函数传递一个参数,在其中确定要启用哪个输入?那么您只需要使用该函数一次。例如,一个带有的数字枚举您的输入,该数字位于输入名称的末尾

差不多

<input type="text" class="form-control" id="name1" value="Skipper" readonly>
<div class="input-group-addon">
  <span class="glyphicon glyphicon-pencil" onclick=editName('1')></span>
</div>

<input type="text" class="form-control" id="name2" value="Skipper" readonly>
<div class="input-group-addon">
  <span class="glyphicon glyphicon-pencil" onclick=editName('2')></span>
</div>

为什么不向javascript函数传递一个参数,以确定在其中启用哪个输入?那么您只需要使用该函数一次。例如,一个带有的数字枚举您的输入,该数字位于输入名称的末尾

差不多

<input type="text" class="form-control" id="name1" value="Skipper" readonly>
<div class="input-group-addon">
  <span class="glyphicon glyphicon-pencil" onclick=editName('1')></span>
</div>

<input type="text" class="form-control" id="name2" value="Skipper" readonly>
<div class="input-group-addon">
  <span class="glyphicon glyphicon-pencil" onclick=editName('2')></span>
</div>
示例1: 您还可以创建一个“编辑”按钮,用户可以单击该按钮以使内容可编辑。以下是此应用程序的HTML:

HTML

<div class='row text-center'>
    <a class="btn btn-danger editBtn">Edit Off</a>
</div>
<form role="form">
    <div class="form-group">
        <label for="name">Name</label>
        <div class="input-group">
            <input type="text" class="form-control editField" value="Skipper" readonly>
            <input type="text" class="form-control editField" value="McAwesome-Sauce" readonly>
            <input type="text" class="form-control editField" value="Tester" readonly>
        </div>
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>
这将允许您切换所有字段进行编辑。它还将使您能够根据自己的喜好定制内容。看看下面我的小提琴,了解更多

示例2: 这个例子将包含更多的视觉设计。它具有示例1的思想,但也包括更改图示符图标。请看以下内容:

jQuery

$(document).ready(function () {
    $('.editBtn').click(function () {
        if ($('.editField').is('[readonly]')) { //checks if it is already on readonly mode
            $('.editField').prop('readonly', false);//turns the readonly off
            $('.editBtn').html('Edit On'); //Changes the text of the button
            $('.editBtn').css("background", "green"); //changes the background of the button
            $('.editBtn').css("border", "green"); //changes the border of the button
        } else { //else we do other things
            $('.editField').prop('readonly', true);
            $('.editBtn').html('Edit Off');
            $('.editBtn').css("background", "red");
        }
    });

});
$(document).ready(function () {
    $('.editBtn').click(function () {
        if ($('.editField').is('[readonly]')) { //checks if it is already on readonly mode
            $('.editField').prop('readonly', false); //turns the readonly off
            $('.editBtn').html('Edit On'); //Changes the text of the button
            $('.editBtn').css("background", "green"); //changes the background of the button
            $('.editBtn').css("border", "green"); //changes the border of the button
            $('.editMode').toggleClass('hide'); //hide one glyphicon
            $('.nonEditMode').addClass('hide'); //show another glyphicon
        } else { //else we do other things
            $('.editField').prop('readonly', true);
            $('.editBtn').html('Edit Off');
            $('.editBtn').css("background", "red");
            $('.editMode').toggleClass('hide');
            $('.nonEditMode').removeClass('hide');
        }
    });

});
$(document).ready(function () {
    $('span').click( function() {
        if($(this).parents().siblings('input').is('[readonly]')){
           $(this).parents().siblings('input').prop('readonly', false); //turns the readonly off
        }else{
           $(this).parents().siblings('input').prop('readonly', true); //turns the readonly off
        }
    });
});
$(document).ready(function () {
    $('.glyphicon-ok').toggleClass('hide');
    $('span').click(function () {
        if ($(this).parents().siblings('input').is('[readonly]')) {
            $(this).parents().siblings('input').prop('readonly', false); //turns the readonly off
            $(this).toggleClass('hide'); //hide one glyphicon
            $(this).siblings('.glyphicon-pencil').toggleClass('hide');
        } else {
            $(this).toggleClass('hide'); //hide one glyphicon
            $(this).siblings('.glyphicon-ok').toggleClass('hide');
            $(this).parents().siblings('input').prop('readonly', true); //turns the readonly off
        }
    });

});
HTML-部分

<label for="name">Name</label>
        <div class="input-group margin-bottom-10">
            <input type="text" class="form-control editField" value="Skipper" readonly />
                <div class="input-group-addon">
                  <span class="glyphicon glyphicon-pencil hide editMode"></span>
                  <span class="glyphicon glyphicon-ok nonEditMode"></span>
                </div>         
        </div>
        <label for="name">Position</label>
        <div class="input-group">
            <input type="text" class="form-control editField" value="Being Awesome" readonly />
                <div class="input-group-addon">
                  <span class="glyphicon glyphicon-pencil hide editMode"></span>
                  <span class="glyphicon glyphicon-ok nonEditMode"></span>
                </div>         
        </div>
示例4: 这可能是你想要的更多。我没有修改您的任何HTML,我只是将
hide
类添加到您的span项目中。您可以在下面的小提琴示例中看到它。以下是本文档中包含的jQuery:

jQuery

$(document).ready(function () {
    $('.editBtn').click(function () {
        if ($('.editField').is('[readonly]')) { //checks if it is already on readonly mode
            $('.editField').prop('readonly', false);//turns the readonly off
            $('.editBtn').html('Edit On'); //Changes the text of the button
            $('.editBtn').css("background", "green"); //changes the background of the button
            $('.editBtn').css("border", "green"); //changes the border of the button
        } else { //else we do other things
            $('.editField').prop('readonly', true);
            $('.editBtn').html('Edit Off');
            $('.editBtn').css("background", "red");
        }
    });

});
$(document).ready(function () {
    $('.editBtn').click(function () {
        if ($('.editField').is('[readonly]')) { //checks if it is already on readonly mode
            $('.editField').prop('readonly', false); //turns the readonly off
            $('.editBtn').html('Edit On'); //Changes the text of the button
            $('.editBtn').css("background", "green"); //changes the background of the button
            $('.editBtn').css("border", "green"); //changes the border of the button
            $('.editMode').toggleClass('hide'); //hide one glyphicon
            $('.nonEditMode').addClass('hide'); //show another glyphicon
        } else { //else we do other things
            $('.editField').prop('readonly', true);
            $('.editBtn').html('Edit Off');
            $('.editBtn').css("background", "red");
            $('.editMode').toggleClass('hide');
            $('.nonEditMode').removeClass('hide');
        }
    });

});
$(document).ready(function () {
    $('span').click( function() {
        if($(this).parents().siblings('input').is('[readonly]')){
           $(this).parents().siblings('input').prop('readonly', false); //turns the readonly off
        }else{
           $(this).parents().siblings('input').prop('readonly', true); //turns the readonly off
        }
    });
});
$(document).ready(function () {
    $('.glyphicon-ok').toggleClass('hide');
    $('span').click(function () {
        if ($(this).parents().siblings('input').is('[readonly]')) {
            $(this).parents().siblings('input').prop('readonly', false); //turns the readonly off
            $(this).toggleClass('hide'); //hide one glyphicon
            $(this).siblings('.glyphicon-pencil').toggleClass('hide');
        } else {
            $(this).toggleClass('hide'); //hide one glyphicon
            $(this).siblings('.glyphicon-ok').toggleClass('hide');
            $(this).parents().siblings('input').prop('readonly', true); //turns the readonly off
        }
    });

});
首先切换其中一个字形图标使其不被隐藏,然后每次鼠标单击跨距时,只读将被删除

示例1: 您还可以创建一个“编辑”按钮,用户可以单击该按钮以使内容可编辑。以下是此应用程序的HTML:

HTML

<div class='row text-center'>
    <a class="btn btn-danger editBtn">Edit Off</a>
</div>
<form role="form">
    <div class="form-group">
        <label for="name">Name</label>
        <div class="input-group">
            <input type="text" class="form-control editField" value="Skipper" readonly>
            <input type="text" class="form-control editField" value="McAwesome-Sauce" readonly>
            <input type="text" class="form-control editField" value="Tester" readonly>
        </div>
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>
这将允许您切换所有字段进行编辑。它还将使您能够根据自己的喜好定制内容。看看下面我的小提琴,了解更多

示例2: 这个例子将包含更多的视觉设计。它具有示例1的思想,但也包括更改图示符图标。请看以下内容:

jQuery

$(document).ready(function () {
    $('.editBtn').click(function () {
        if ($('.editField').is('[readonly]')) { //checks if it is already on readonly mode
            $('.editField').prop('readonly', false);//turns the readonly off
            $('.editBtn').html('Edit On'); //Changes the text of the button
            $('.editBtn').css("background", "green"); //changes the background of the button
            $('.editBtn').css("border", "green"); //changes the border of the button
        } else { //else we do other things
            $('.editField').prop('readonly', true);
            $('.editBtn').html('Edit Off');
            $('.editBtn').css("background", "red");
        }
    });

});
$(document).ready(function () {
    $('.editBtn').click(function () {
        if ($('.editField').is('[readonly]')) { //checks if it is already on readonly mode
            $('.editField').prop('readonly', false); //turns the readonly off
            $('.editBtn').html('Edit On'); //Changes the text of the button
            $('.editBtn').css("background", "green"); //changes the background of the button
            $('.editBtn').css("border", "green"); //changes the border of the button
            $('.editMode').toggleClass('hide'); //hide one glyphicon
            $('.nonEditMode').addClass('hide'); //show another glyphicon
        } else { //else we do other things
            $('.editField').prop('readonly', true);
            $('.editBtn').html('Edit Off');
            $('.editBtn').css("background", "red");
            $('.editMode').toggleClass('hide');
            $('.nonEditMode').removeClass('hide');
        }
    });

});
$(document).ready(function () {
    $('span').click( function() {
        if($(this).parents().siblings('input').is('[readonly]')){
           $(this).parents().siblings('input').prop('readonly', false); //turns the readonly off
        }else{
           $(this).parents().siblings('input').prop('readonly', true); //turns the readonly off
        }
    });
});
$(document).ready(function () {
    $('.glyphicon-ok').toggleClass('hide');
    $('span').click(function () {
        if ($(this).parents().siblings('input').is('[readonly]')) {
            $(this).parents().siblings('input').prop('readonly', false); //turns the readonly off
            $(this).toggleClass('hide'); //hide one glyphicon
            $(this).siblings('.glyphicon-pencil').toggleClass('hide');
        } else {
            $(this).toggleClass('hide'); //hide one glyphicon
            $(this).siblings('.glyphicon-ok').toggleClass('hide');
            $(this).parents().siblings('input').prop('readonly', true); //turns the readonly off
        }
    });

});
HTML-部分

<label for="name">Name</label>
        <div class="input-group margin-bottom-10">
            <input type="text" class="form-control editField" value="Skipper" readonly />
                <div class="input-group-addon">
                  <span class="glyphicon glyphicon-pencil hide editMode"></span>
                  <span class="glyphicon glyphicon-ok nonEditMode"></span>
                </div>         
        </div>
        <label for="name">Position</label>
        <div class="input-group">
            <input type="text" class="form-control editField" value="Being Awesome" readonly />
                <div class="input-group-addon">
                  <span class="glyphicon glyphicon-pencil hide editMode"></span>
                  <span class="glyphicon glyphicon-ok nonEditMode"></span>
                </div>         
        </div>
示例4: 这可能是你想要的更多。我没有修改您的任何HTML,我只是将
hide
类添加到您的span项目中。您可以在下面的小提琴示例中看到它。以下是本文档中包含的jQuery:

jQuery

$(document).ready(function () {
    $('.editBtn').click(function () {
        if ($('.editField').is('[readonly]')) { //checks if it is already on readonly mode
            $('.editField').prop('readonly', false);//turns the readonly off
            $('.editBtn').html('Edit On'); //Changes the text of the button
            $('.editBtn').css("background", "green"); //changes the background of the button
            $('.editBtn').css("border", "green"); //changes the border of the button
        } else { //else we do other things
            $('.editField').prop('readonly', true);
            $('.editBtn').html('Edit Off');
            $('.editBtn').css("background", "red");
        }
    });

});
$(document).ready(function () {
    $('.editBtn').click(function () {
        if ($('.editField').is('[readonly]')) { //checks if it is already on readonly mode
            $('.editField').prop('readonly', false); //turns the readonly off
            $('.editBtn').html('Edit On'); //Changes the text of the button
            $('.editBtn').css("background", "green"); //changes the background of the button
            $('.editBtn').css("border", "green"); //changes the border of the button
            $('.editMode').toggleClass('hide'); //hide one glyphicon
            $('.nonEditMode').addClass('hide'); //show another glyphicon
        } else { //else we do other things
            $('.editField').prop('readonly', true);
            $('.editBtn').html('Edit Off');
            $('.editBtn').css("background", "red");
            $('.editMode').toggleClass('hide');
            $('.nonEditMode').removeClass('hide');
        }
    });

});
$(document).ready(function () {
    $('span').click( function() {
        if($(this).parents().siblings('input').is('[readonly]')){
           $(this).parents().siblings('input').prop('readonly', false); //turns the readonly off
        }else{
           $(this).parents().siblings('input').prop('readonly', true); //turns the readonly off
        }
    });
});
$(document).ready(function () {
    $('.glyphicon-ok').toggleClass('hide');
    $('span').click(function () {
        if ($(this).parents().siblings('input').is('[readonly]')) {
            $(this).parents().siblings('input').prop('readonly', false); //turns the readonly off
            $(this).toggleClass('hide'); //hide one glyphicon
            $(this).siblings('.glyphicon-pencil').toggleClass('hide');
        } else {
            $(this).toggleClass('hide'); //hide one glyphicon
            $(this).siblings('.glyphicon-ok').toggleClass('hide');
            $(this).parents().siblings('input').prop('readonly', true); //turns the readonly off
        }
    });

});
首先切换其中一个字形图标使其不被隐藏,然后每次鼠标单击跨距时,只读将被删除


为什么不向javascript函数传递一个参数,以确定在其中启用哪个输入?那么您只需要使用该功能一次。您是否考虑过只添加一个按钮来为用户打开/关闭编辑功能?Shri,请查看下面的两个答案,看看它们是否回答了您的问题。如果其中一人确实回答了您的问题,则单击复选标记以批准该问题。如果可以的话,让我们结束这个问题。为什么不向javascript函数传递一个参数,在其中确定要启用哪个输入?那么您只需要使用该功能一次。您是否考虑过只添加一个按钮来为用户打开/关闭编辑功能?Shri,请查看下面的两个答案,看看它们是否回答了您的问题。如果其中一人确实回答了您的问题,则单击复选标记以批准该问题。如果可以的话,让我们结束这个问题。谢谢。这对我很管用。此外,我还可以通过将glyphicon更改为OK&修改javascript函数,使元素在单击新图标时再次成为只读。谢谢。这对我很管用。此外,我还可以将glyphicon改为OK&修改javascript函数,使元素在单击新图标时再次成为只读。