用jquery获取html数组元素

teu*_*ara 6 jquery jquery-selectors

我有一个HTML数组;

<input class="input_box" name="authors[]" id="author1"/>
Run Code Online (Sandbox Code Playgroud)

我想要做的是,在没有刷新页面的情况下获取此数组的值,(我确实在输入字段旁边显示值,独立div)

$(document).ready(function(){    
$("input[id^="author"]").change(update);
});
    function update(){ 
    var authors=new Array();
    $("input[id^="author"]").each(function(){authors.push($(this).text()) }); 
    var author= '"' + authors.join('", "') + '"';

    $('#val-authors').html('authors:<span class="red"> "'+author+'"</span>');}
Run Code Online (Sandbox Code Playgroud)

我唯一看到的<div id="val-authors">是,"",

我错过了什么?

提前致谢..

Aln*_*tak 7

你的选择器:

$("input[id^="author"]")
Run Code Online (Sandbox Code Playgroud)

是不正确的,因为你试图在双引号使用双引号.其中一对需要是单引号.

如果您使用的是JS调试器,则应立即显示.

FWIW,这是一种检索所需数组的更简洁的方法:

var authors = $('input[id^="author"]').map(function() {
  return this.value;
}).get();
Run Code Online (Sandbox Code Playgroud)