简体   繁体   English

按文本选择 jquery 中的选项

[英]Select option in jquery by text

This should be a fairly simple task but all the solutions online never worked for me.这应该是一项相当简单的任务,但所有在线解决方案都不适合我。

I have an array that contains loan types:我有一个包含贷款类型的数组:

var arrLoanType = {
        1:"Regular Loan",
        2:"Emergency Loan",
        3:"Special Loan",
        ...}

I compare the above to entries in the database, so that the ones existing in the database are removed from the array.我将上述内容与数据库中的条目进行比较,以便将数据库中存在的条目从数组中删除。

I've looped through them via the code below:我通过下面的代码遍历它们:

$.each(arrLoanType, function(index, value){
    $("#loanType").append("<option value='"+index+"'>"+value+"</option>");
});

Now to the question, what I want to happen is to have a default selected field based on the option's text.现在的问题是,我想要发生的是基于选项文本的默认选定字段。 This is my code:这是我的代码:

$('#loanType option').filter(function () { 
   return $(this).text() == selectedLoan; 
}).attr('selected','selected');

where selectedLoan is from a hidden input field var selectedLoan = $("#loanSelected").val();其中selectedLoan来自隐藏的输入字段var selectedLoan = $("#loanSelected").val();

It just doesn't work.它只是不起作用。 This is how my HTML code structure looks like after the $.each is generated:这是生成$.each后我的 HTML 代码结构的样子:

<select name="" id="loanType" class="form-control" required="">
    <option value="" selected="" disabled="" hidden="">Select Loan Type...</option>
    <option value="1">Regular Loan</option>
    <option value="2">Emergency Loan</option>
    <option value="3">Special Loan</option>
...

I already tried removing "selected" attribute on the first item but it still doesn't work.我已经尝试删除第一个项目上的“selected”属性,但它仍然不起作用。 Any help would be appreciated.任何帮助,将不胜感激。 Thanks.谢谢。

you can select the default value on creation itself您可以选择创建本身的默认值

$.each(arrLoanType, function(index, value){
    if(value == selectedLoan)
        $("#loanType").append("<option value='"+index+"' selected>"+value+"</option>");
    else
        $("#loanType").append("<option value='"+index+"'>"+value+"</option>");
});

the if else checks the text is equal to selected loan or not and add selected attribute in creation itself I think it solves your problem if else 检查文本是否等于选定的贷款并在创建中添加选定的属性我认为它解决了您的问题

Seems fine please have a look.看起来不错,请看一下。 Any error in the console?控制台有什么错误吗? perhaps you forget to include jquery lib or not inside doc ready?也许您忘记在文档中包含 jquery lib 或未准备好?

 $(document).ready(function(e) { var arrLoanType = { 1:"Regular Loan", 2:"Emergency Loan", 3:"Special Loan"} $.each(arrLoanType, function(index, value){ $("#loanType").append("<option value='"+index+"'>"+value+"</option>"); }); $('#loanType option').filter(function () { return $(this).text() == $("#loanSelected").val(); }).attr('selected','selected'); });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <select name="" id="loanType" class="form-control" required=""></select> <input id ="loanSelected" value="Special Loan" type="hidden">

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

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