多个复选框,一键提交php表单

时间:2019-02-09 19:43:47

标签: javascript jquery ajax forms

有没有办法改善这一点?无法找到改善方法。

matchingItemsVote = [];
var allBtnElements = document.getElementsByTagName('button');
for (i=0; i < allBtnElements.length; i++) {
    try {
        var exists = allBtnElements[i].getAttribute("aria-label");
        if (exists == null) { continue; }
    } catch {
        continue;
    }
    if (allBtnElements[i].getAttribute("aria-label").includes("upvote")) {
        matchingItemsVote.push(allBtnElements[i]);
    }
}
for (i = 0; i < matchingItemsVote.length; i++) {
    matchingItemsVote[i].nextSibling.innerHTML = "Votes";
}

无法弄清楚使其更快的最佳方法是什么。 有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

尝试这种方式,删除循环

 $(document).on('submit','#submit-form',function(e){
           e.preventDefault();
    
           var checkedBOX = $('#checkboxes').find('input:checked');
           var servers = [];
            $("input:checkbox[name=checkbox]:checked").each(function(){
              servers.push($(this).val());
            });
  
              console.log(servers);
              
             $.ajax({
               type: "POST",
               url: window.location.href,
               data: $('#submit-form').serialize() + '&server=' + servers + '&submit=',
               success:function(data){
                  window.location.reload();
               }
             });
           
       });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>


<form method="POST" id="submit-form">
<input type="text" name="name" placeholder="NAME"/>
<br/>
<input type="text" name="email" placeholder="EMAIL"/>
<br/>

<input type="checkbox" name="checkbox" value="1" />
<input type="checkbox" name="checkbox" value="2" />
<input type="checkbox" name="checkbox" value="3" />
<input type="checkbox" name="checkbox" value="4" />
<br/>
<button type="submit">SUBMIT</button>
</form>