[英]Jquery mobile interfering with javascript filling select box
我正在尝试使用javascript以编程方式填充html选择框。 我发现尽管我的代码可以正常工作,但jQuery mobile正在干扰选择框中显示的内容。 我正在设置选定的选项,该选项正在起作用,但是选择框中显示的值是错误的值。 它并没有改变我最初从html中获得的内容。
我链接到:“ http://code.jquery.com/mobile/1.3.0/jquery.mobile-1.3.0.min.css ”。 无论如何有绕过jQuery的? 我不能只是删除jQuery CSS。 那会破坏我网站上的所有内容,而我没有时间来全部重做。
这是问题的jsfiddle: http : //jsfiddle.net/RjXRB/1/
这是我的代码供参考:
这是我的javascript函数,它会填充选择框:
function printCartList(newCart){
// newCart is an optional parameter. Check to see if it is given.
newCart = newCart ? newCart : "a_new_cart_was_not_provided_12345abcde";
// Create a cart list from the stored cookie if it exists, otherwise create a new one.
if($.cookie("carts") != null){
carts = JSON.parse($.cookie("carts"));
}
else{
selectOption = new Object();
selectOption.value = "newuniquecartid12345abcde";
selectOption.html = "***New Cart***";
carts = new Object();
carts.size = 1;
carts.option = new Array();
carts.option[0] = selectOption;
}
// Get the select box
var select = document.getElementById("select_cart");
// Get the number of options in the select box.
var length = select.options.length;
// Remove all of the options in the select box.
while(select.options.length > 0){
select.remove(0);
}
// If there is a new cart, add it to the cart.
if(newCart != "a_new_cart_was_not_provided_12345abcde"){
selectOption = new Object();
selectOption.value = newCart;
selectOption.html = newCart;
carts.option[carts.size] = selectOption;
carts.size++;
}
// Save the cart in a cookie
$.cookie("carts",JSON.stringify(carts));
// Populate the select box
for(var i = 0; i < carts.size; i++){
var opt = document.createElement('option');
opt.value = carts.option[i].value;
opt.innerHTML = carts.option[i].html;
if($.cookie("activeCart") == carts.option[i].value && newCart != "a_new_cart_was_not_provided_12345abcde"){
// Set the selected value
alert(carts.option[i].value);
opt.selected = true;
// I tried changing the value of a p tag inside the default option, but that didn't work
document.getElementById("selectHTML").innerHTML = carts.option[i].html;
}
if(opt.value == "dd"){alert("hi");};
select.appendChild(opt);
}
}
这是我的html:
<form method="POST" name="cartSelectForm" action="home.php">
<select name="cartList" id="select_cart" data-mini="true" onchange="submitCartForm()" data-icon="false">
<option value="newuniquecartid1234567890"><p id="selectHTML">*** New Cart ***</p></option>
</select>
</form>
任何帮助将不胜感激。
我知道这个问题大约有一个月了,但是我偶然发现了这个问题,因为我遇到了完全相同的问题。 我最终通过执行以下操作修复了该问题:
通过编程设置该值后,我在该选择框中手动触发了更改事件。 我猜想它迫使jQuery mobile更新与该元素相关的所有增强标记。 有点烦人,但幸运的是很容易解决。
$("#country").val("US");
$("#country").change();
希望能帮助到别人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.