在链式选择框上应用 jquery 选择框样式
我在页面中创建了一对链接的选择框。第二个选择框填充了一组值,具体取决于第一个框的选定值。
使两个选择框像这样工作的脚本使用 PHP 和 JavaScript。这是我正在使用的代码:
表单
<select name="continent" tabindex="1" onChange="getCountry(this.value)">
<option value="#">-Select-</option>
<option value="Europe">Europe</option>
<option value="Asia">Asia</option>
</select>
<div id="countrydiv">
<select name="country" tabindex="2">
<option></option>
</select>
</div>
<input type="submit" />
</form>
javascript 代码
$(document).ready(function() {
$('select[name="continent"]').selectbox({debug: true});
$('select[name="country"]').selectbox({debug: true});
});
function getXMLHTTP() { //function to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getCountry(continentId) {
var strURL="findCountry.php?continent="+continentId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('countrydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
php 代码(findCountry.php)
<?
$continent=intval($_GET['continent']);
if ($_GET['continent'] == 'Europe') {
?>
<select name="country">
<option value="France">France</option>
<option value="Germany">Germany</option>
<option value="Spain">Spain</option>
<option value="Italy">Italy</option>
</select>
<? }
if ($_GET['continent'] == 'Asia') {
?>
<select name="country">
<option value="China">China</option>
<option value="India">India</option>
<option value="Japan">Japan</option>
</select>
<? } ?>
我想要做的是在这些选择框上应用 jQuery 选择框样式。我还没有成功做到这一点。问题是 jQuery 隐藏了普通的选择框并用列表替换它。此外,刷新selectbox的内容后,jquery无法将其重新构建为列表。您可以在此处查看 jQuery 代码
我可以做些什么来结合这些技术吗?我已经尝试了一百万件事,但没有任何效果。你能帮我吗?
I have created a pair of chained selectboxes in my page. The second selectbox is filled with a set of values, depending on the first box's selected value.
The script that makes the two selectboxes work like this, uses PHP and JavaScript. This is the code I'm using:
form
<select name="continent" tabindex="1" onChange="getCountry(this.value)">
<option value="#">-Select-</option>
<option value="Europe">Europe</option>
<option value="Asia">Asia</option>
</select>
<div id="countrydiv">
<select name="country" tabindex="2">
<option></option>
</select>
</div>
<input type="submit" />
</form>
javascript code
$(document).ready(function() {
$('select[name="continent"]').selectbox({debug: true});
$('select[name="country"]').selectbox({debug: true});
});
function getXMLHTTP() { //function to return the xml http object
var xmlhttp=false;
try{
xmlhttp=new XMLHttpRequest();
}
catch(e) {
try{
xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e){
try{
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e1){
xmlhttp=false;
}
}
}
return xmlhttp;
}
function getCountry(continentId) {
var strURL="findCountry.php?continent="+continentId;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('countrydiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
php code (findCountry.php)
<?
$continent=intval($_GET['continent']);
if ($_GET['continent'] == 'Europe') {
?>
<select name="country">
<option value="France">France</option>
<option value="Germany">Germany</option>
<option value="Spain">Spain</option>
<option value="Italy">Italy</option>
</select>
<? }
if ($_GET['continent'] == 'Asia') {
?>
<select name="country">
<option value="China">China</option>
<option value="India">India</option>
<option value="Japan">Japan</option>
</select>
<? } ?>
What I want to do is to apply jQuery selectbox styling on these selectboxes. I haven't succeeded in doing that yet. The problem is that jQuery is hiding the normal selectbox and is replacing it with a list. Furthermore, after selectbox's content is refreshed, jquery cannot re-construct it into a list. You can take a look of the jQuery code here
Is there something I can do to combine these techniques? I have tried a million things but nothing worked. Can you please help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你正在使用 jQuery,对吗?!
所以,让我们用 jQuery 的方式来做...
form
jQuery
php 代码 (findCountry.php) php)
you are using jQuery, right?!
So, lets do this jQuery way....
form
jQuery
php code (findCountry.php)
在 ajax 调用结束时,您需要重新分配 selectbox() 调用。您遇到的问题是,您在页面加载时设置的 DOM 元素在 ajax 调用结束时消失了,取而代之的是新的选择框。如果您在 jQuery 中使用 $.ajax 或 $.get 方法,它们将为您提供完成回调的选项。我会用那个。
编辑:这有效。
at the end of your ajax call you need to reassign the selectbox() call. The problem you are experiencing is that DOM elements you setup on page load are gone by the end of your ajax call, replaced with a new select box. If you use the $.ajax or $.get methods in jQuery they give you the option of a completion callback. I would use that.
Edit: This works.