简体   繁体   English

如何在jQuery Mobile中显示之前设置页面中选择框的值?

[英]How to set value of select box in page before show in jquery mobile?

How to set value of select box in page before show in jquery mobile. 如何在jQuery Mobile中显示之前设置页面中选择框的值。 i have given normally as 我通常给

document.getElementById('RA_IF_inpVisible').value = visible;

In page it is given as follows 在页面上给出如下

<div data-role="fieldcontain">
     <label for="name">Visibility:</label>
     <select name="RA_IF_inpVisible" id="RA_IF_inpVisible">
            <option value="0">0</option>
            <option value="1">1</option>
     </select>
</div>
</fieldset>

If the variable is "1" also it is showing only "0" as default value. 如果变量为“ 1”,则它也仅显示“ 0”作为默认值。

You need to use .val() to set value and then refresh selectmenu to apply changes. 您需要使用.val()设置值,然后刷新选择.val()以应用更改。

$(document).on("pagecontainerbeforeshow", function (e, data) {
  if ( data.toPage[0].id == "PageID" ) {
    $("#RA_IF_inpVisible", data.toPage).val(1).selectmenu("refresh");
  }
});

Demo 演示

If i got it right, this should help you: 如果我做对了,这应该对您有帮助:

 var e = document.getElementById('RA_IF_inpVisible'); //Value is the Value of your Page, so you need to set it in some way ;) var value = 1; var i=0; for (i;i<e.options.length; i++){ if(e.options[i].value==value){ e.options[i].selected = true; } } e.style.visibility = "visible"; 
 <div data-role="fieldcontain"> <label for="name">Visibility:</label> <select name="RA_IF_inpVisible" id="RA_IF_inpVisible"> <option value="0">0</option> <option value="1">1</option> </select> </div> 

This Javascript should circle through all elements and compare your value which should be set to the values of the options. 该Javascript应该遍历所有元素,并比较您应将其值设置为选项的值。 If they match, the options will be selected. 如果它们匹配,则将选择选项。 Afterwards your select will be visible. 之后,您的选择将可见。

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

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