根据选择一个下拉列表动态填充下拉列表

发布于 2024-12-11 02:53:07 字数 3401 浏览 0 评论 0原文

我正在尝试根据另一个下拉列表的选择来填充一个下拉列表。我正在使用struts框架并使用jquery来解决上述问题陈述。我的代码卡在使用 .json() 方法对服务器进行 GET 调用的地方。有人可以帮助我解决这个问题吗?

我的 jquery 代码:

<div id="Symptoms" style="display:none;">
<html:form id="iamhere" method="POST" action="symptoms" >
<p></p>
<p>Enter/Choose ailment : 

<select id="diselc" name="AuthoringForm"  size="1" >
<option selected="selected"> </option>
<option>Malaria</option>
<option>High Fever</option>
<option>Cholera</option>
<option>Diarrhoea</option>
</select></p>
<p>Choose ailment classification : 
<select id="diselchild" name="AuthoringForm" > 
<option>High</option>
<option>Medium</option>
<option>Low</option> 
</select> 



<fieldset style="width: 381px; height: 126px; padding: 2">
<legend align="left"></legend>
 Tick off patient context :
<html:radio value="Men" name="AuthoringForm" property="patient_context" disabled="false"/>
Men
<html:radio value="Womwen" name="AuthoringForm" property="patient_context" disabled="false"/>
Women
<p>
<html:radio value="Child" name="AuthoringForm" property="patient_context" disabled="false"/>
Child
<html:radio value="Al" name="AuthoringForm" property="patient_context" disabled="false"/>
All
</fieldset>
<p>Enter Pre Conditions</p>
<p><html:textarea rows="2" name="AuthoringForm" cols="20" property="patient_precondition" ></html:textarea>
<p>Must Have Symptoms : 
May Have Symptoms :</p>
<p><html:textarea rows="2" name="AuthoringForm" cols="20" property="must_have_symptoms"></html:textarea>
<!-- <input type="submit" value="Submit" name="B2">-->
<html:textarea rows="2" name="AuthoringForm" cols="20" property="may_have_symptoms"></html:textarea>
<input type="submit" value="Submit" name="symptomsButton"><input type="reset" value="Reset" onclick="clear_form_elements(this.form);"></p>
</html:form>
</div>

我的 jquery:

<script> 
            $(document).ready(function() { 
                $("#diselc").change(function(){ 
                    fillOptions('diselc','diselchild'); 
                    }); 
            }); 
            function fillOptions(parentId,ddId) {
                alert("atleast the call is being made in the fillOptions"+ddId);
                var dd = $('#' + ddId); 
                alert("the value being passed is "+dd);
                var jsonURL = 'http://localhost:8080/docRuleTool/decisiontreeAction.do?dd=' + ddId + '&val=' + $('#' + parentId + ' :selected').val(); 
                $.getJSON(jsonURL, function(opts)
                {
                alert("just after calling the servlet stuff ");
                    $('>option', dd).remove(); // Clean old options first. 
                    if (opts) { 
                        $.each(opts, function(key, value) { 
                            dd.append($('<option/>').val(key).text(value)); 
                        }); 
                    } else { 
                        dd.append($('<option/>').text("Please select parent")); 
                    } 
                }); 
            } 
 </script> 

有人可以帮助我吗?

I am trying to populate one drop down based on selection of another drop down . I am using struts framework and used jquery to solve the above problem statement . my code is getting stuck at the point where it makes a GET call to the server using .json() method .Can someone help me on this .

my jquery code :

<div id="Symptoms" style="display:none;">
<html:form id="iamhere" method="POST" action="symptoms" >
<p></p>
<p>Enter/Choose ailment : 

<select id="diselc" name="AuthoringForm"  size="1" >
<option selected="selected"> </option>
<option>Malaria</option>
<option>High Fever</option>
<option>Cholera</option>
<option>Diarrhoea</option>
</select></p>
<p>Choose ailment classification : 
<select id="diselchild" name="AuthoringForm" > 
<option>High</option>
<option>Medium</option>
<option>Low</option> 
</select> 



<fieldset style="width: 381px; height: 126px; padding: 2">
<legend align="left"></legend>
 Tick off patient context :
<html:radio value="Men" name="AuthoringForm" property="patient_context" disabled="false"/>
Men
<html:radio value="Womwen" name="AuthoringForm" property="patient_context" disabled="false"/>
Women
<p>
<html:radio value="Child" name="AuthoringForm" property="patient_context" disabled="false"/>
Child
<html:radio value="Al" name="AuthoringForm" property="patient_context" disabled="false"/>
All
</fieldset>
<p>Enter Pre Conditions</p>
<p><html:textarea rows="2" name="AuthoringForm" cols="20" property="patient_precondition" ></html:textarea>
<p>Must Have Symptoms : 
May Have Symptoms :</p>
<p><html:textarea rows="2" name="AuthoringForm" cols="20" property="must_have_symptoms"></html:textarea>
<!-- <input type="submit" value="Submit" name="B2">-->
<html:textarea rows="2" name="AuthoringForm" cols="20" property="may_have_symptoms"></html:textarea>
<input type="submit" value="Submit" name="symptomsButton"><input type="reset" value="Reset" onclick="clear_form_elements(this.form);"></p>
</html:form>
</div>

My jquery:

<script> 
            $(document).ready(function() { 
                $("#diselc").change(function(){ 
                    fillOptions('diselc','diselchild'); 
                    }); 
            }); 
            function fillOptions(parentId,ddId) {
                alert("atleast the call is being made in the fillOptions"+ddId);
                var dd = $('#' + ddId); 
                alert("the value being passed is "+dd);
                var jsonURL = 'http://localhost:8080/docRuleTool/decisiontreeAction.do?dd=' + ddId + '&val=' + $('#' + parentId + ' :selected').val(); 
                $.getJSON(jsonURL, function(opts)
                {
                alert("just after calling the servlet stuff ");
                    $('>option', dd).remove(); // Clean old options first. 
                    if (opts) { 
                        $.each(opts, function(key, value) { 
                            dd.append($('<option/>').val(key).text(value)); 
                        }); 
                    } else { 
                        dd.append($('<option/>').text("Please select parent")); 
                    } 
                }); 
            } 
 </script> 

Can someone help me on that .

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赏烟花じ飞满天 2024-12-18 02:53:07

正如 @meloncholy 所提到的,进行错误回调,

$.getJSON(jsonURL, function() {
 //your code here
  alert("success");
})
.error(function(jxhr) { alert(jxhr.ResponseText); });

看看错误处理程序是否被调用......

as mentioned by @meloncholy make an error callback

$.getJSON(jsonURL, function() {
 //your code here
  alert("success");
})
.error(function(jxhr) { alert(jxhr.ResponseText); });

see if the error handler gets called...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文