options [options.length]无效

0 html javascript jquery attributes

我有jquery代码,谁取值并在数据库中更新它们.问题是它现在似乎不起作用.

它说:未捕获的TypeError:无法读取未定义的属性"长度"

以下是代码的一部分:

$('#response_title').val('');
$("#response_new_enter").hide();
$("#response_own_enter").show();
img.attr("src","images/add_btn.png");
var options = $('#response_').attr('options');
options[options.length] = new Option(response_title, r, true, true); // error comes up in this line
alert("New Response Added Successfully.");
$("#myspan").html("New Response Added Successfully.");
$("#myspan").show();
setTimeout(function(){ $('#myspan').fadeOut(1000); }, 10000);
Run Code Online (Sandbox Code Playgroud)

Aru*_*hny 6

options 不是属性,是尝试的属性

var options = $('#response_').prop('options');
var options = $('#response_')[0].options; //or
Run Code Online (Sandbox Code Playgroud)

但相反,您可以使用jQuery创建类似的选项

$('<option />',{text: response_title, value: r, selected: true}).appendTo('#response_');
Run Code Online (Sandbox Code Playgroud)