JQuery Mobile:使用 javascript 更新选择

发布于 2024-10-25 10:40:08 字数 2562 浏览 1 评论 0原文

我正在尝试在phonegap/JQM 应用程序中使用javascript 更改选择值。 我按照建议在更改其值后调用 .selectmenu('refresh') ;它收到此错误:

未捕获无法在初始化之前调用 selectmenu 上的方法;尝试调用方法“刷新”

如果我删除该调用,选择上的 .val() 将更改,但屏幕上的图形不会更改。 .selectmenu("refresh") 旨在将图形与选择的 .val() 同步。

以下是我尝试利用的两个资源: http://jquerymobile.com/demos/1.0a3/#docs /forms/plugin-eventsmethods.html http://jquerymobile.com/test/docs/forms/forms-selects.html

我是新的phonegap 和JQM。

以下是尝试每三秒翻转一次选择的示例代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"    "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="viewport" content="width=default-width; user-scalable=no" />

<meta http-equiv="Content-type" content="text/html; charset=utf-8">

<title>Demo Error</title>

<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">

function onBodyLoad()
{
       document.addEventListener("deviceready",onDeviceReady,false);
       for (i=1;i<10;i++) {
          setTimeout('changeProduct()',i*3000);
       }
}
    function changeProduct()
    {
       if ($("#product").val() == 'S')
       {
          $("#product").val('M');
       }
       else
       {
          $("#product").val('S');
       }
       console.log("calling selectmenu refesh change to " + $("#product").val());
       $("#product").selectmenu('refresh', true);
    }

/* When this function is called, PhoneGap has been initialized and is ready to roll */
function onDeviceReady()
{
    // do your thing!
}

</script>
<link rel="stylesheet" href="jquery.mobile-1.0a3.css" />
<link rel="stylesheet" href="quote.css" />
<script src="jquery-1.5.min.js"></script>
<script src="jquery.mobile-1.0a3.js"></script>

</head>
<body onload="onBodyLoad()">
<div id="page1" data-role="page">
<div data-role="content">
    <div id="product-all" data-role="fieldcontain">
      <label for="product">Product:</label>
  <select data-role="slider" name="product" id="product" value="S">
    <option id="one" value="S">Single</option>
    <option id="two" value="M" selected="selected">Multi</option>
  </select>
    </div>
</div>
</div>
</body>  
</html>

I am trying change a select value with javascript in a phonegap/JQM application.
I followed the recommendation to call .selectmenu('refresh') after I changed its value; It get this error:

Uncaught cannot call methods on selectmenu prior to initialization; attempted to call method 'refresh'

If I remove that call the .val() on the select will change, but the graphic on the screen will not change. The .selectmenu("refresh") is intended to synchronize the graphic with the .val() of the select.

Here are two of the resources I am attempting to make use of:
http://jquerymobile.com/demos/1.0a3/#docs/forms/plugin-eventsmethods.html
http://jquerymobile.com/test/docs/forms/forms-selects.html

I am new phonegap and JQM.

Here is sample code that attempts to flips the select every three seconds:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"    "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta name="viewport" content="width=default-width; user-scalable=no" />

<meta http-equiv="Content-type" content="text/html; charset=utf-8">

<title>Demo Error</title>

<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
<script type="text/javascript" charset="utf-8">

function onBodyLoad()
{
       document.addEventListener("deviceready",onDeviceReady,false);
       for (i=1;i<10;i++) {
          setTimeout('changeProduct()',i*3000);
       }
}
    function changeProduct()
    {
       if ($("#product").val() == 'S')
       {
          $("#product").val('M');
       }
       else
       {
          $("#product").val('S');
       }
       console.log("calling selectmenu refesh change to " + $("#product").val());
       $("#product").selectmenu('refresh', true);
    }

/* When this function is called, PhoneGap has been initialized and is ready to roll */
function onDeviceReady()
{
    // do your thing!
}

</script>
<link rel="stylesheet" href="jquery.mobile-1.0a3.css" />
<link rel="stylesheet" href="quote.css" />
<script src="jquery-1.5.min.js"></script>
<script src="jquery.mobile-1.0a3.js"></script>

</head>
<body onload="onBodyLoad()">
<div id="page1" data-role="page">
<div data-role="content">
    <div id="product-all" data-role="fieldcontain">
      <label for="product">Product:</label>
  <select data-role="slider" name="product" id="product" value="S">
    <option id="one" value="S">Single</option>
    <option id="two" value="M" selected="selected">Multi</option>
  </select>
    </div>
</div>
</div>
</body>  
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪冰清 2024-11-01 10:40:08

我想通了:

当你想使用javascript更改 data-role=slider 的值时,更改该值后,请调用: $('#mysliderid').slider('refresh');

当您想使用 javascript 更改 data-role=select 的值时,更改值后,请调用: $("#myselectid").selectmenu('refresh', true);

这可能会令人困惑,因为它们在内部都使用 select,所以人们可能会被误导,认为 selectmenu 对两者都适用。

I figured it out:

When you want to change a value of a data-role=slider using javascript, after you change the value, call : $('#mysliderid').slider('refresh');

When you want to change a value of a data-role=select using javascript, after you change the value, call : $("#myselectid").selectmenu('refresh', true);

This can be confusing because internally they both use select so one might be misled into thinking that selectmenu would work for both.

一袭水袖舞倾城 2024-11-01 10:40:08

对我来说,在 JQM 1.4 上,摆脱以下问题的唯一方法:
“无法在初始化之前调用 selectmenu 上的方法;尝试调用方法“刷新””
是使用 data-role="fieldcontain" 在我的切换器周围创建一个包装器父级,然后在调用我的切换器的 之前调用该元素的 .trigger("create") >.slider("refresh")

换句话说,我从这个: 更改

<select id='policy_toggler' data-role='slider'>
  <option value="false">No</option>
  <option value="true">Yes</option>
</select>

<script>
$("#policy_toggler").selectmenu("refresh");  //rises prior-to-initialization error, nothing happens visually.
</script>

为:

<div class="prefparam" data-role="fieldcontain">
  <select id='policy_toggler' data-role='slider'>
    <option value="false">No</option>
    <option value="true">Sí</option>
  </select>
</div>

<script>
$(".prefparam").trigger("create");
$("#policy_toggler").slider("refresh");  //visual sync between real input element and visual ui element
</script>

另请注意,在使用 JQM1.4 时,使用 .slider() 访问切换器的 API,而不是.selectmenu()

For me, on JQM 1.4, the only way to get rid of:
"Cannot call methods on selectmenu prior to initialization; attempted to call method 'refresh' "
was to create a wrapper parent around my toggler with data-role="fieldcontain" then, call that element's .trigger("create") just before calling my toggler's .slider("refresh")

In other words, I changed from this:

<select id='policy_toggler' data-role='slider'>
  <option value="false">No</option>
  <option value="true">Yes</option>
</select>

<script>
$("#policy_toggler").selectmenu("refresh");  //rises prior-to-initialization error, nothing happens visually.
</script>

to this:

<div class="prefparam" data-role="fieldcontain">
  <select id='policy_toggler' data-role='slider'>
    <option value="false">No</option>
    <option value="true">Sí</option>
  </select>
</div>

<script>
$(".prefparam").trigger("create");
$("#policy_toggler").slider("refresh");  //visual sync between real input element and visual ui element
</script>

Also note, when working with JQM1.4 a toggler's API is accessed with .slider(), not .selectmenu()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文