简体   繁体   English

jQuery General函数获取:input选择器的值

[英]jQuery General function to get value of :input selector

Is there a way to get the value of an :input in jQuery that holds for all :input ? 有没有办法得到一个值:input jQuery中保存的所有:input

I am asking this because I have a page with select and checkbox , it is for the following code: 我问这个问题是因为我有一个带有selectcheckbox的页面,它用于以下代码:

    for (var i = 0; i < arguments.length; i++) {
        var localArgument = arguments[i].trim();
        data[localArgument] = $(html).find(":input[name='" + localArgument + "']").val();
        $(html).on("change", ":input[name='" + localArgument + "']", function(event) {
            console.log(localArgument + ": " + $(this).val());
            data[localArgument] = $(this).val();
            reloadTable(table, html, data);
        });
    }

Where arguments is an array that holds names for elements. 其中arguments是一个保存元素名称的数组。

I know I need to do it for checkbox with .prop("checked") , however I would much rather use a general function which I know does not need to be updated in the future. 我知道我需要使用.prop("checked") checkbox来完成此操作,但是我宁愿使用一个通用函数,该函数我知道以后不需要进行更新。

just use $('input') selector to select all input elements 只需使用$('input')选择器来选择所有input元素

If you want to get the value of each input element you can do something like 如果要获取每个输入元素的值,可以执行以下操作

$('input').each(function() {
   // use $(this).val() to get the value
})

You can safely use val() for all inputs, checkboxes, radio buttons, and selects. 您可以安全地将val()用于所有输入,复选框,单选按钮和选择。 This is a demo: http://jsfiddle.net/FxjQB/3 这是一个演示: http : //jsfiddle.net/FxjQB/3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM