[英]Removing all values but the first from a drop down list using jQuery
if (questions[0]) {
$("select[id$=ddlPollQuestions] > option").remove();
$('#ddlPollQuestions').append('<option value="">Choose a question to compare to</option>');
$.each(questions, function(i, question) {
$('#ddlPollQuestions').append('<option value="' + question.QUESTIONID + '">' + question.TEXT + '</option>');
});
} else {
$("select[id$=ddlPollQuestions] > option").remove();
$('#ddlPollQuestions').append('<option value="' + 0 + '">' + 'There are no questions of this type' + '</option>');
}
What this does is it removes all the previous values, But i want my first option, which is "Choose a question..." to remain, and then display "There are not questions..." as my 2nd option.它的作用是删除所有以前的值,但我希望我的第一个选项,即“选择一个问题......”保留,然后显示“没有问题......”作为我的第二个选项。 My code here does not show "Choose a question.." as the first option.
我的代码没有将“选择一个问题..”作为第一个选项。 Thanks for having a look!
感谢您的关注!
An easier approach:更简单的方法:
$('#ddlPollQuestions').children('option:not(:first)').remove();
Try this(Note that I have added a string variable to append the options after the each loop for better performance):试试这个(注意我在每个循环之后的选项中添加了一个字符串变量 append 以获得更好的性能):
if (questions[0]) {
$("select[id$=ddlPollQuestions] > option:gt(0)").remove();
$('#ddlPollQuestions').append('<option value="">Choose a question to compare to</option>');
var options = "";
$.each(questions, function(i, question) {
options += '<option value="' + question.QUESTIONID + '">' + question.TEXT + '</option>';
});
$('#ddlPollQuestions').append(options);
} else {
$("select[id$=ddlPollQuestions] > option:gt(0)").remove();
$('#ddlPollQuestions').append('<option value="' + 0 + '">' + 'There are no questions of this type' + '</option>');
}
since you are removing all the options in your code, you'll might just want to append again the Choose a question...
in your else
statement由于您要删除代码中的所有选项,因此您可能只想在
else
语句中再次选择 append Choose a question...
if (questions[0]) {
$("select[id$=ddlPollQuestions] > option").remove();
$('#ddlPollQuestions').append('<option value="">Choose a question to compare to</option>');
$.each(questions, function(i, question) {
$('#ddlPollQuestions').append('<option value="' + question.QUESTIONID + '">' + question.TEXT + '</option>');
});
} else {
$("select[id$=ddlPollQuestions] > option").remove();
$('#ddlPollQuestions').append('<option value="">Choose a question to compare to</option>');
$('#ddlPollQuestions').append('<option value="' + 0 + '">' + 'There are no questions of this type' + '</option>');
}
example: http://jsfiddle.net/xeYwv/2/例如: http://jsfiddle.net/xeYwv/2/
var $PollQuestions = $('#ddlPollQuestions');
if (questions[0]) {
$PollQuestions.children().remove();
$PollQuestions.append('<option value="0">Choose a question to compare to</option>');
$.each(questions, function(i, question) {
$PollQuestions.append('<option value="' + question.QUESTIONID + '">' + question.TEXT + '</option>');
});
} else {
$PollQuestions.children().remove();
$PollQuestions.append('<option value="' + 0 + '">' + 'There are no questions of this type' + '</option>');
}
what you had is already pretty close.你所拥有的已经非常接近了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.