最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to clear a input array all elements using jQuery - Stack Overflow

programmeradmin2浏览0评论

I have defined my form elements using the html array method like this:

<input type="text" maxlength="45" name="Apikey[key]">
<select name="Apikey[developer]">

How can I clear all Apikey array using jQuery?

The question is how to clear them? All possible forms fields must be cleared.

I have defined my form elements using the html array method like this:

<input type="text" maxlength="45" name="Apikey[key]">
<select name="Apikey[developer]">

How can I clear all Apikey array using jQuery?

The question is how to clear them? All possible forms fields must be cleared.

Share Improve this question edited Jan 29, 2011 at 17:12 Pentium10 asked Jan 27, 2011 at 22:17 Pentium10Pentium10 208k124 gold badges435 silver badges511 bronze badges 4
  • The question is to reset them? That's just setting $("el").val("");. But you have special characters in your jQuery selection string which will fail. – Jonathon Faust Commented Jan 27, 2011 at 22:25
  • select elements don't go back when using val("") – Pentium10 Commented Jan 27, 2011 at 22:26
  • You haven't asked about select elements. – Jonathon Faust Commented Jan 27, 2011 at 22:27
  • I don't want a reset. I want all form elements to have no value. Checkboxes cleared, selects to -1 etc... – Pentium10 Commented Jan 29, 2011 at 18:10
Add a ment  | 

5 Answers 5

Reset to default 3

Try ^(starts with) in the selector instead of |. Also to reset the fields appropriately storing the initial values using data. i.e:

$(function(){
    $("input[name^=Apikey]").each(function(){
      var $this = $(this);
      $this.data("initial-val", $this.val());
    });
});
.
.
.
function resetApiValues(){
   $("input[name^=Apikey]").each(function(){
     var $this = $(this);
     $this.val($this.data("initial-val"));
   });
}

The easiest thing to do would be to use the "standard" form plugin and call clearForm() or clearFields():

$('#formId').clearForm();
$('#formId [name^=Apikey]').clearFields();

Or, if you don't want or need all the extra form plugin machinery, just grab the source and figure out how clearForm() and clearFields() work. clearFields() is actually quite simple:

$.fn.clearFields = $.fn.clearInputs = function() {
    return this.each(function() {
        var t = this.type, tag = this.tagName.toLowerCase();
        if (t == 'text' || t == 'password' || tag == 'textarea') {
            this.value = '';
        }
        else if (t == 'checkbox' || t == 'radio') {
            this.checked = false;
        }
        else if (tag == 'select') {
            this.selectedIndex = -1;
        }
    });
};

here you go:

$("input[name^='Apikey']").attr("name", "");

to clear the values:

$("input[name^='Apikey']").val("");

I see that you have edited to include different HTML elements, in that case:

$("*[name^='Apikey']")).each(function() { 
    switch(this.type) { 
        case 'password': 
        case 'select-multiple': 
        case 'select-one': 
        case 'text': 
        case 'textarea': 
            $(this).val(''); 
            break; 
        case 'checkbox': 
        case 'radio': 
            this.checked = false; 
    } 
}); 

$('input[name^="Apikey"]')...

See the official jQuery API here. bye

You cant just clear all the values. Some elements like SELECT have to have a value.

发布评论

评论列表(0)

  1. 暂无评论