如何从表单中的所有输入复制自定义属性,然后再赋值回来

How to copy custom attribute from all inputs in the form and then assign back?

本文关键字:自定义属性 复制 然后 回来 赋值 输入 表单      更新时间:2023-09-26

我有一个表单有很多输入(文本,选择框,提交等)。表单上有2个按钮,当第一个按钮被点击时,我想从所有输入复制自定义属性,如果第二个按钮被点击,我想分配这些属性,如果自定义属性为空(=")。

我现在拥有的:

  • 含有数十个inputs的形式(#myForm)
  • 如果输入没有一个命名为.dontcheck的特定类,则取消自定义属性赋值的函数

    $(':input','#myForm')
    .not(':button, :submit, :reset, :hidden, .dontcheck')
    .attr("customAttribute","");
    

所以,当第一个按钮被点击时,我应该复制所有的输入' customAttribute和清除customAttribute,如果它没有.dontcheck类。我有干净的部分,但我不知道如何复制,然后将customAttribute分配回每个输入与它自己唯一的id

我是JQuery世界的新手,任何建议或帮助都很感激。

  var   customAttrList;
 function deleteCustomAttr(){   var elementToDeleteAttr = $(':input','#myForm')
  .not(':button, :submit, :reset, :hidden, .dontcheck');
 customAttrList = {};
  $.each(elementToDeleteAttr, function(index, item){
         customAttrList [item.id] = $(item).attr("customAttribute");//copy attributes and save by Id
         $(item).attr("customAttribute", "");
  });

}
 function returnAttrBack(){
       var elementToBackAttr = $(':input','#myForm')
  .not(':button, :submit, :reset, :hidden, .dontcheck');
 $.each(elementToBackAttr , function(index, item){
         $(item).attr("customAttribute", customAttrList[item.id]);//get attribute by Id
  });
 }