简体   繁体   English

<option>不附加<select>在jquery

[英]<option> not append in <select> in jquery

I have tried to append <option> inside <select> using javascript by parsing data from XMl. 我试图通过解析来自XMl的数据,使用javascript在<select>附加<option> Data parsed from xml properly,but it is not appending in <select> . 从xml正确解析的数据,但它没有附加在<select>

Also I want to show option as (image+text) format. 另外,我想将选项显示为(图像+文本)格式。 How to do this.... 这个怎么做....

My HTML code: 我的HTML代码:

<form style="margin:20px 0">
    <p>
        <select id='mySelect' multiple="multiple" style="width:370px">
        </select>
    </p>

</form>

Javascript code: Javascript代码:

$(document).ready(function(){

    $.ajax({
        type: "GET",
        url: "newpersonal1.xml",
        dataType: "xml",
        success: function(xml) {
            var select = $('#mySelect');
            $(xml).find('value').each(function(){
                 var title = $(this).find('name').text();
                 alert(title);
                 select.append("<option value='"+title+"'>"+title+"</option>");         
            });                 
        }
    });
});

How to fix this issue and add image in select <option> . 如何解决此问题并在select <option>添加图像。

The below code worked for me. 以下代码对我有用。

$(function(){
   $.ajax({
    type: "GET",
    url: "newpersonal1.xml",
    dataType: "xml",
    success: function(xml) {
        var select = $('#mySelect');
        var options = '';
        $(xml).find('value').each(function(){
             var title = $(this).find('name').text();
             options += "<option value='" + title + "'>" + title + "</option>";                         
        });             
        select.html(options);
        $("#mySelect").multiselect().multiselectfilter();
    }
  });   
});

I noticed that the xml file does not load in chrome when html run from a local folder. 我注意到当从本地文件夹运行html时,xml文件不会加载到chrome中。 But it does work on a server. 但它确实可以在服务器上运行。

Mark as answer if helpful. 如果有帮助,请标记为答案 Cheers 干杯

You have to make additional steps when you deal with "filter and multiselect plugin" in your way. 当你以自己的方式处理“过滤和多选插件”时,你必须采取额外的步骤。

It mimics the select object, hiding the original one and creating the set of button and span elements. 它模仿选择对象,隐藏原始对象并创建按钮和跨度元素集。 It uses the original one only at the moment of initialization. 它仅在初始化时使用原始的。

So, you have to call function refresh() as soon as you update the original select tag with additional options. 因此,只要使用其他选项更新原始选择标记,就必须调用函数refresh()。

http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/#refresh http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/#refresh

It's possible that your title string is malformed and requires escaping. 您的title字符串可能格式错误,需要转义。

Concatenating strings isn't a good way to create elements unless you're absolutely sure the values can't contain any special characters. 除非您完全确定值不能包含任何特殊字符,否则连接字符串不是创建元素的好方法。 A better approach therefore would be: 因此,更好的方法是:

$('<option>', { val: title, text: title }).appendTo(select);

I'd note that it's not actually necessary to specify a value in an <option> tag if the desired value is the same as the text contained within the option. 我注意到,如果所需的值与选项中包含的文本相同,则实际上不必在<option>标记中指定值。

Try this may help you, 试试这可能对你有帮助,

   var selectList = "", title = ""; 
   $(xml).find('value').each(function(){ 
    title = $(this).find('name').text(); 
    selectList += "<option value='" + title + "'>" + title + "</option>";
   });
   $('#mySelect').html(selectList);  

this code is work in my case don't know for you.... 这段代码是在我的情况下工作,不知道为你....

try 尝试

$( select ).append("<option value='"+title+"'>"+title+"</option>");

as you can see, I'm surrounding "select" with $( and ) 正如你所看到的,我正在用$(和)围绕“选择”

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

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