我必须动态创建一个单选按钮组。我能够成功创建它。但问题是动态创建的单选按钮看起来不像静态按钮。
我尝试了两种方法:
1)create
- fieldset
类上的被叫ui-corner-all
被添加到内部跨度上,因此所有单选按钮都显示为圆形。
2)在包含create
的div上调用fieldset
- 这里问题更好。但是仍然存在一些问题。首先,当只有单选按钮时,ui-controlgroup-last
类被添加到那个radiobutton.Next,当添加第二个单选按钮时,ui-controlgroup-last
类被添加到该单选按钮。但是ui-controlgroup-last
仍然存在于第一个单选按钮中,并且此循环重复。作为结果所有单选按钮都具有最后一个单选按钮的样式。
这是我的代码:
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.js"></script>
<script>
$(document).ready(function()
{
for (var i = 0; i < 3; i++)
{
$("#rgroup1").append('<input type="radio" name="rg1" id="radio1-'+i+'"/><label for="radio1-'+i+'">Value</label>');
$("#rgroup1").trigger("create");
$("#rgroup2").append('<input type="radio" name="rg2" id="radio2-'+i+'"/><label for="radio2-'+i+'">Value</label>');
$("#radiodiv1").trigger("create");
}
});
</script>
</head>
<body>
<div data-role="content">
<h3>Normal radio buttons</h3>
<div id="radiodiv">
<fieldset data-role="controlgroup" id="rgroup">
<input type="radio" name="rg" id="radio-1"/><label for="radio-1">Value</label>
<input type="radio" name="rg" id="radio-2"/><label for="radio-2">Value</label>
<input type="radio" name="rg" id="radio-3"/><label for="radio-3">Value</label>
</fieldset>
</div>
<h3>Dynamically created-Div Refresh</h3>
<div id="radiodiv">
<fieldset data-role="controlgroup" id="rgroup1">
</fieldset>
</div>
<h3>Dynamically created-Fieldset Refresh</h3>
<div id="radiodiv1">
<fieldset data-role="controlgroup" id="rgroup2">
</fieldset>
</div>
</div>
</body>
P.S - 我在每次追加后调用create的原因是因为它在我的实际用例中是如何工作的 - 任何时候都只创建一个单选按钮。
生成的来源
<html lang="en" class="ui-mobile"><head><base href="http://localhost:8081/html5/calendar-new/radio_issue.html">
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.css">
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.js"></script>
<script>
$(document).ready(function()
{
for (var i = 0; i < 3; i++)
{
$("#rgroup1").append('<input type="radio" name="rg1" id="radio1-'+i+'"/><label for="radio1-'+i+'">Value</label>');
$("#rgroup1").trigger("create");
$("#rgroup2").append('<input type="radio" name="rg2" id="radio2-'+i+'"/><label for="radio2-'+i+'">Value</label>');
$("#radiodiv1").trigger("create");
}
});
</script>
<title></title></head>
<body class="ui-mobile-viewport"><div data-role="page" data-url="/html5/calendar-new/radio_issue.html" tabindex="0" class="ui-page ui-body-c ui-page-active" style="min-height: 933px; ">
<div data-role="content" class="ui-content" role="main">
<h3>Normal radio buttons</h3>
<div id="radiodiv">
<fieldset data-role="controlgroup" id="rgroup" class="ui-corner-all ui-controlgroup ui-controlgroup-vertical">
<div class="ui-radio"><input type="radio" name="rg" id="radio-1"><label for="radio-1" data-theme="c" class="ui-btn ui-btn-icon-left ui-radio-off ui-corner-top ui-btn-up-c"><span class="ui-btn-inner ui-corner-top"><span class="ui-btn-text">Value</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow"></span></span></label></div>
<div class="ui-radio"><input type="radio" name="rg" id="radio-2"><label for="radio-2" data-theme="c" class="ui-btn ui-btn-icon-left ui-radio-off ui-btn-up-c"><span class="ui-btn-inner"><span class="ui-btn-text">Value</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow"></span></span></label></div>
<div class="ui-radio"><input type="radio" name="rg" id="radio-3"><label for="radio-3" data-theme="c" class="ui-btn ui-btn-icon-left ui-radio-off ui-corner-bottom ui-controlgroup-last ui-btn-up-c"><span class="ui-btn-inner ui-corner-bottom ui-controlgroup-last"><span class="ui-btn-text">Value</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow"></span></span></label></div>
</fieldset>
</div>
<h3>Dynamically created-Div Refresh</h3>
<div id="radiodiv">
<fieldset data-role="controlgroup" id="rgroup1" class="ui-corner-all ui-controlgroup ui-controlgroup-vertical">
<div class="ui-radio"><input type="radio" name="rg1" id="radio1-0"><label for="radio1-0" data-theme="c" class="ui-btn ui-btn-icon-left ui-btn-corner-all ui-radio-off ui-btn-up-c"><span class="ui-btn-inner ui-btn-corner-all"><span class="ui-btn-text">Value</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow"></span></span></label></div><div class="ui-radio"><input type="radio" name="rg1" id="radio1-1"><label for="radio1-1" data-theme="c" class="ui-btn ui-btn-icon-left ui-btn-corner-all ui-radio-off ui-btn-up-c"><span class="ui-btn-inner ui-btn-corner-all"><span class="ui-btn-text">Value</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow"></span></span></label></div><div class="ui-radio"><input type="radio" name="rg1" id="radio1-2"><label for="radio1-2" data-theme="c" class="ui-btn ui-btn-icon-left ui-btn-corner-all ui-radio-off ui-btn-up-c"><span class="ui-btn-inner ui-btn-corner-all"><span class="ui-btn-text">Value</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow"></span></span></label></div></fieldset>
</div>
<h3>Dynamically created-Fieldset Refresh</h3>
<div id="radiodiv1">
<fieldset data-role="controlgroup" id="rgroup2" class="ui-corner-all ui-controlgroup ui-controlgroup-vertical">
<div class="ui-radio"><input type="radio" name="rg2" id="radio2-0"><label for="radio2-0" data-theme="c" class="ui-btn ui-btn-icon-left ui-radio-off ui-corner-top ui-corner-bottom ui-controlgroup-last ui-btn-up-c"><span class="ui-btn-inner ui-corner-top ui-corner-bottom ui-controlgroup-last"><span class="ui-btn-text">Value</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow"></span></span></label></div><div class="ui-radio"><input type="radio" name="rg2" id="radio2-1"><label for="radio2-1" data-theme="c" class="ui-btn ui-btn-icon-left ui-radio-off ui-corner-bottom ui-controlgroup-last ui-btn-up-c"><span class="ui-btn-inner ui-corner-bottom ui-controlgroup-last"><span class="ui-btn-text">Value</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow"></span></span></label></div><div class="ui-radio"><input type="radio" name="rg2" id="radio2-2"><label for="radio2-2" data-theme="c" class="ui-btn ui-btn-icon-left ui-radio-off ui-corner-bottom ui-controlgroup-last ui-btn-up-c"><span class="ui-btn-inner ui-corner-bottom ui-controlgroup-last"><span class="ui-btn-text">Value</span><span class="ui-icon ui-icon-radio-off ui-icon-shadow"></span></span></label></div></fieldset>
</div>
</div>
</div><div class="ui-loader ui-body-a ui-corner-all" style="top: 290px; "><span class="ui-icon ui-icon-loading spin"></span><h1>loading</h1></div></body></html>
答案 0 :(得分:2)
我通过明确删除除最后一个单选按钮的标签之外的所有标签中的ui-controlgroup-last
和ui-corner-bottom
类来修复此问题
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.css" />
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script src="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.js"></script>
<script>
$(document).ready(function()
{
for (var i = 0; i < 3; i++)
{
/*$("#rgroup1").append('<input type="radio" name="rg1" id="radio1-'+i+'"/><label id="label1-'+i+'" for="radio1-'+i+'">Value</label>');
$("#rgroup1").trigger("create");*/
$("#rgroup2").append('<input type="radio" name="rg2" id="radio2-'+i+'"/><label id="label2-'+i+'"for="radio2-'+i+'">Value</label>');
$("#radiodiv1").trigger("create");
}
for (var i = 0; i < 2; i++)
{
$('#label2-'+i).removeClass("ui-controlgroup-last ui-corner-bottom");
}
});
</script>
</head>
<body>
<div data-role="content">
<h3>Normal radio buttons</h3>
<div id="radiodiv">
<fieldset data-role="controlgroup" id="rgroup">
<input type="radio" name="rg" id="radio-1"/><label for="radio-1">Value</label>
<input type="radio" name="rg" id="radio-2"/><label for="radio-2">Value</label>
<input type="radio" name="rg" id="radio-3"/><label for="radio-3">Value</label>
</fieldset>
</div>
<!-- <h3>Dynamically created-Div Refresh</h3>
<div id="radiodiv">
<fieldset data-role="controlgroup" id="rgroup1">
</fieldset>
</div> -->
<h3>Dynamically created-Fieldset Refresh</h3>
<div id="radiodiv1">
<fieldset data-role="controlgroup" id="rgroup2">
</fieldset>
</div>
</div>
</body>
答案 1 :(得分:1)
您需要刷新按钮..
刷新单选按钮
如果您通过JavaScript操作单选按钮,则必须在其上调用refresh方法以更新视觉样式。这是一个例子:
$("input[type='radio']").attr("checked",true).checkboxradio("refresh");
http://jquerymobile.com/demos/1.0a4.1/docs/forms/forms-radiobuttons.html
答案 2 :(得分:0)
由于您使用的是jquery,我建议您使用
$(“#radio1 - ”+ i)。addClass(“classname”)
答案 3 :(得分:0)
如果有人遇到这个问题,那么应用当前版本(1.4.5)的样式的正确方法是enhanceWithin
在我编写的一段代码中,在创建了一些动态html之后,我追加它,然后应用样式:
var currentAddressInput = new AddressInterface();
jQuery("#contentDiv").append(currentAddressInput.domElement).enhanceWithin();