jQuery Mobile - 设置下拉选择框/选项的值

20

我正在尝试使用jQuery Mobile设置select/option的值,但好像无法正常工作。

<!-- Complete example below. -->

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="jquery.mobile/jquery.mobile.css" /> 
    <script type="text/javascript" src="jquery.mobile/jquery.js"></script>
    <script type="text/javascript" src="jquery.mobile/jquery.mobile.js"></script>
</head>
<body>

<div data-role="page" id="mainmenu">
    <div data-role="header" data-position="inline">
    <h1>Main Menu</h1>
    </div>
    <div class="ui-body ui-body-c">
    <div data-role="content">   
        <div id='placeholderA' ></div>  
        <div id='placeholderB' ></div>  
    <div class="ui-block-b"><button type="submit"  id="addPart" data-theme="a" data-icon="plus">Add Serial/Part</button></div>
    </div>
</body>
</html>         

<script>        

    var currentTab = "A";       

    // An XML fragment
    testXML =  "<?xml version='1.0' encoding='UTF-8' ?>\
    <Doc>\
        <DtlFields>\
            <ACTC>B</ACTC>\
            <QTY>5</QTY>\
        </DtlFields>\
        <DtlFields>\
            <ACTC>A</ACTC>\
            <QTY>6</QTY>\
        </DtlFields>\
    </Doc>";

    // An HTML fragment
    section = "<ul data-role='listview' data-theme='a'>\
            <li>PART: <span class='thisSection'></span>\
            <div data-role='fieldcontain'>\
                <label>A Label</label>\
                <select name='ACTC' id='preAction' data-theme='a'>\
                <option value='A'>A</option>\
                <option value='B'>B</option>\
                <option value='C'>C</option>\
                </select>\
            </div>\
            </li>\
            </ul>\
            <!-- ***   Quantity     *** -->\
            <div data-role='fieldcontain'>\
                <label>QTY</label>\
                <input type='range' name='QTY' id='preQuant01' value='1' min='1' max='10'/>\
            </div>\
        </div>";


$(document).ready(function () {

    /* Add a listeners to ADD PART */   
        $('#addPart').click(function() {                                
            var xml = $($.parseXML(testXML));           
            xml.find("DtlFields").each(function () {
                var XMLString= $(this);             
                fnAddPart(XMLString);                               
            });     
            return false;
        });   

    // add a part section to a Group on screen
    function fnAddPart(XMLString){
        myTmpl = $(section);                                                    

        if (XMLString != "" ){

            // set Quantity - this works
            var x =((XMLString).find("QTY").text());
            myTmpl.find("input[name='QTY']").attr('value', x);          

            // ************ set Activity - but this does not work! ***************
            var x =((XMLString).find("ACTC").text());           
            myTmpl.find("input[name='ACTC']").attr('value', x); 

        }       
        // append to page
        myTmpl.appendTo("#placeholder"+currentTab).page();                                                      
    }
});

</script>       
2个回答

68

在 jQuery Mobile 中通过编程方式操作像 select 字段这样的元素时,一旦您做出了相关选择,您需要刷新元素以便更新用户界面。以下是一个示例代码片段,它设置了 select 字段中的一个值,然后对其进行更新:

// Grab a select field
var el = $('#YOUR_SELECT_FIELD');

// Select the relevant option, de-select any others
el.val('some value').attr('selected', true).siblings('option').removeAttr('selected');

// jQM refresh
el.selectmenu("refresh", true);

所以我怀疑你需要的是最后那一行。


el.val('some value') 这不是返回 select 元素本身而不是选项吗?至少对我来说是这样的。 - antitoxic
不:var el = $('#YOUR_SELECT_FIELD'); 是<select>组。el.val('some value')可让您得到该<select>中值为“some value”的<option> - Sparkle
为这个问题被两个反对票所逗乐—— 都是在事件发生三年后的同一天。 - Ben
真的很棒的一行代码。 - Caleb Pitman
是的。尝试了其他20种选项,最终这个成功了。 - deebs
如果您想触发选择列表的 onchange 处理程序,请在末尾执行 el.change(); - tyler.frankenstein

5
在某些情况下,您可能需要包装您的函数以在文档准备就绪时执行并初始化selectmenu。以下是我使用Ben Poole的示例提供的解决方案:
$(document).ready(function(){
// Grab a select field
var el = $('#YOUR_SELECT_FIELD');

// Select the relevant option, de-select any others
el.val('some value').attr('selected', true).siblings('option').removeAttr('selected');

// Initialize the selectmenu
el.selectmenu();

// jQM refresh
el.selectmenu("refresh", true);
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接