gpt4 book ai didi

javascript - 为什么 jQuery Mobile 选择框会锁定选项?

转载 作者:行者123 更新时间:2023-11-27 23:44:14 24 4
gpt4 key购买 nike

我创建了两个选择框,一个依赖于另一个,要选择一个“用户”,您需要选择一个“区域”,因此当该“区域”出现时,具有该“区域”的所有“用户”都将可用已选择。当不使用 jQuery Mobile 时它工作得很好,但是当我放置链接并测试它时,它会锁定选项。为什么要这样做?

任何帮助将不胜感激!

更新:奇怪的是,它实际上获取了它所选择的选项的值!但它并没有“刷新”选择 带有所选选项文本的标签。当选择为空时,在控制台上尝试 $('#user').val();

要查看错误,请在第一个选择框中选择测试,然后尝试在第二个选择框中选择user

在这里尝试:http://liveweave.com/1hOGCg

我的代码(尝试删除 jQuery Mobile 链接以查看其工作情况):

localStorage.setItem('dad', '[[1,"asdasd"],[2,"Test"]]');
localStorage.setItem('son', '[[1,1,"Test"],[10,1,"User213"],[2,1,"Test2"],[3,1,"Test3"],[4,1,"Test4"],[5,1,"Test5"],[6,1,"Testtesttest6"],[7,1,"Test7"],[8,1,"Test8"],[9,2,"User"]]');
var dadArray = (localStorage.getItem('dad') !== null) ? JSON.parse(localStorage.getItem('dad')) : '';
var sonArray = (localStorage.getItem('son') !== null) ? JSON.parse(localStorage.getItem('son')) : '';
var tempArray = '';

if (localStorage.getItem('dad') === null && localStorage.getItem('son') === null) {
getData();
}

function getDad(dad, id) {
var size = dad.length;
for (var i = 0; i < size; i++) {
$(id).append('<option value="' + dad[i][0] + '">' + dad[i][1] + '</option>');
}
}

function getSon(son, id, dad) {
var size = son.length;
for (var i = 0; i < size; i++) {
if (dad == son[i][1]) {
$(id).append('<option class="area' + son[i][1] + '" value="' + son[i][0] + '">' + son[i][2] + '</option>');
}
}
}

$('#areas').change(function() {
$('#user').empty();
getSon(sonArray, '#user', $(this).val());
});

$(function() {
getDad(dadArray, '#areas');
$('#areas').change();
});

//If localStorage has been erased
/*
function getData(){
tempArray = $.ajax({
url: 'http://sistema.agrosys.com.br/webpro/webadm/wcgsynua',
async: false
});
tempArray = tempArray.responseText;
splitArray(tempArray,dadArray,sonArray);
}

function splitArray(array,dad, son){
var isItDad = true;
for(var i=0;i<array.length;i++){
var bit = array.substring(i, i+1);
if(bit == 's'){
if(array.substring(i, i+4) == 'son='){
isItDad = false;
}
}
if(isItDad === true){
dad += bit;
}else{
son += bit;
}
}
dadArray = dad;
sonArray = son;
dadArray = dadArray.replace('dad=', '');
sonArray = sonArray.replace('son=', '');
dadArray = dadArray.replace(',[]', '');
sonArray = sonArray.replace(',[]', '');
dadArray = JSON.parse(dadArray);
sonArray = JSON.parse(sonArray);
}
*/
//If localStorage has been erased
<!DOCTYPE html>
<html>

<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" />
<script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" />
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<title>iterateSelectBox</title>
</head>

<body>
<select id="areas">
</select>
<select id="user">
</select>
</body>

</html>

最佳答案

修改选择后,您需要告诉 jQM 刷新小部件:

http://api.jquerymobile.com/selectmenu/#method-refresh

只需添加

$(id).selectmenu( "refresh", true );

在 getDad() 和 getSon() 的末尾

function getDad(dad, id) {
var size = dad.length;
for (var i = 0; i < size; i++) {
$(id).append('<option value="' + dad[i][0] + '">' + dad[i][1] + '</option>');
}
$(id).selectmenu( "refresh", true );
}

function getSon(son, id, dad) {
var size = son.length;
for (var i = 0; i < size; i++) {
if (dad == son[i][1]) {
$(id).append('<option class="area' + son[i][1] + '" value="' + son[i][0] + '">' + son[i][2] + '</option>');
}
}
$(id).selectmenu( "refresh", true );
}

关于javascript - 为什么 jQuery Mobile 选择框会锁定选项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33389959/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com