How to get input type using jquery?

后端 未结 11 1805
猫巷女王i
猫巷女王i 2020-11-28 02:17

I have a page where the input type always varies, and I need to get the values depending on the input type. So if the type is a radio, I need to get which is checked, and if

11条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-28 02:39

    If what you're saying is that you want to get all inputs inside a form that have a value without worrying about the input type, try this:

    Example: http://jsfiddle.net/nfLfa/

    var $inputs = $('form').find(':checked,:selected,:text,textarea').filter(function() {
        return $.trim( this.value ) != '';
    });
    

    Now you should have a set of input elements that have some value.

    You can put the values in an array:

    var array = $inputs.map(function(){
        return this.value;
    }).get();
    

    Or you could serialize them:

    var serialized = $inputs.serialize();
    

提交回复
热议问题