【问题标题】:unable to set values of select tag in jquery mobile无法在 jquery mobile 中设置选择标记的值
【发布时间】:2011-07-08 18:04:09
【问题描述】:

我在 html(SelectTest.html) 中创建了一个带有一些值的选择标签。我要做的是从选择标签中选择任何值后,我需要刷新页面并将所选选项作为参数传递到网址。

然后我需要在页面刷新后将其设置为选定值。我不确定这个问题是否与 jQuery mobile 有关。

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.css" /> 
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.min.js"></script> 
<script type="text/javascript" src="http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.js"></script> 
<script type="text/javascript">
$(document).ready(function(){

    var param = window.location.search;

    if(param)
    {


      var indx = param.indexOf("=");

        //now get the value of selected type

        aval =param.substring(indx+1);

        $("#lang").val(aval);
    }   

    $("#lang").change(function(){

        var sel = $(this).val();


         window.location.href="SelectTest.html?type="+sel;

        });

});



</script>
<title>Insert title here</title>
</head>
<body>
<div data-role="page" id="home" data-theme="e"> 
<div data-role="header"> 
<h1>Test</h1> 
</div>
<div data-role="content" id="toi">

<div>
<label for="lang">Select Language</label>
<select name="language" id="lang">
<option value="en">English</option>
<option value="hi">Hindi</option>
<option value="sp">Spanish</option>
<option value="fr">French</option>
</select>
</div>



<div id="main">

</div>
</div>
</div> 
<div data-role="footer"> 
<h4>Test</h4> 
</div> 
</div>
</body>
</html>

【问题讨论】:

  • 在 Chrome 上为我工作,所以是的,这可能是移动设备上的问题
  • 是吗?即使我正在尝试使用 chrome,但没有运气。即使在页面刷新后选项是否也会更改。对我来说,每次只有英语选项被选中
  • 它适用于每个选项。也许您的页面中还有其他内容?
  • 嗯,我不明白为什么它对我不起作用..无论如何,在按照乔纳森的建议进行刷新后,它对我有用。感谢您的所有帮助。

标签: jquery jquery-mobile


【解决方案1】:

来自 jquery 移动文档:

如果您通过以下方式操作选择 JavaScript,你必须调用刷新 方法来更新视觉 造型。这是一个例子:

var myselect = $("select#foo");
myselect[0].selectedIndex = 3;
myselect.selectmenu("refresh");

所以基本上你只需要调用刷新就可以了。这有帮助吗?

对于您的代码,您可以像这样链接它

$('#lang').val(aval).selectmenu("refresh");

【讨论】:

  • 没问题!我对 Sliders 有同样的问题......滑块的语法略有不同。我相信是.slider("refresh")
猜你喜欢
  • 2011-10-27
  • 2011-09-11
  • 1970-01-01
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
  • 2016-11-22
  • 2012-04-10
相关资源
最近更新 更多