Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

jquery - 如何在更改时使用源对象在jquery中的表单中清空名称属性(How to empty the name attribute in form in jquery using source object on change)

I am trying to change the name to null/empty of all checkboxes which is unchecked on onchange event and on ondocumentready event using jquery.

(我试图将所有复选框的名称更改为null / empty,这在使用ondocumentready onchange事件和ondocumentready事件中未选中。)

this is my code

(这是我的代码)

<div id="tab_4">
 <div class="col-xs-12">
  <input type="text" name="txtbox1" value="1">
  <input type="checkbox" name="chkbox1" value="1">
  <input checked type="checkbox" name="chkbox2" value="2">
  <input type="checkbox" name="chkbox3" value="3">
 </div>
</div>

<script type="text/javascript">
    $(document).ready(function(){
      $('#tab_4 input[type=checkbox]').each(function(i){
       if($(i).not(':checked'))
        {
         $(this).attr('name', '');
        }
       });
    });
    $('#tab_4 input[type=checkbox]').change(function(){
    $('#tab_4 input[type=checkbox]').each(function(i){
        if($(i).not(':checked'))
        {
         $(this).attr('name', '');
        }
       });
    });
</script>

But it makes all name attribute to null even checked ones .

(但这会使所有name属性都变为null, 即使选中的也是如此 。)

I used this answer Changing input name using JQUERY

(我用这个答案使用JQUERY更改输入名称)

  ask by Danial212k translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You don't need to remove checkbox elements name to exclude it from form data.

(您无需删除复选框元素名称即可将其从表单数据中排除。)

It is excluded by default.

(默认情况下将其排除。)

Check the following demonstration:

(检查以下演示:)

 const tell = () => console.log([... new FormData($('form').get(0)).entries()]) $(() => { tell() $('input').on('change', tell) }) 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <form> <div id="tab_4"> <div class="col-xs-12"> <input type="text" name="txtbox1" value="1"> <input type="checkbox" name="chkbox1" value="1"> <input checked type="checkbox" name="chkbox2" value="2"> <input type="checkbox" name="chkbox3" value="3"> </div> </div> </form> 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...