Javascript 选择由2次jquery选择的选项无法正常工作

Javascript 选择由2次jquery选择的选项无法正常工作,javascript,jquery,ajax,Javascript,Jquery,Ajax,//当ajax请求触发时,我会以相同的形式追加数据,但选择框值未选中。它在firebug中显示正确,但未在屏幕上显示。第一次工作正常时将单击功能代码更改为以下。要重置“选择”,只需将其值设置为空即可 $(".treatment-btn").click(function() { $("#treatmentModal").modal('show'); $('.treatment-form')[0].reset(); $('#treatmentModal #treatment_

//当ajax请求触发时,我会以相同的形式追加数据,但选择框值未选中。它在firebug中显示正确,但未在屏幕上显示。第一次工作正常时

将单击功能代码更改为以下。要重置“选择”,只需将其值设置为空即可

$(".treatment-btn").click(function()
{
    $("#treatmentModal").modal('show');
    $('.treatment-form')[0].reset();
    $('#treatmentModal #treatment_time option').removeAttr("selected");
    $('#treatmentModal #treatment_time option[value=""]').attr("selected", "selected");
}); //when i click on this button it resets form successfully but select option is not reset.

function makeEditHtml(response) {
var price = response[0].treatment_price.split(".")[0];
var cent = response[0].treatment_price.split(".")[1];
$("#treatmentModal #treatment").val(response[0].treatment_name);
$("#treatmentModal #price").val(price);
$('#treatmentModal #cent option[value='+cent+']').attr("selected", "selected");
$('#treatmentModal #treatment_time option').removeAttr("selected");
$('#treatmentModal #treatment_time option[value='+response[0].treatment_duration+']').attr("selected", "selected"); //i select it again at here after ajax call
$.each(response, function(i)
{
    $("#treatmentModal input[type=checkbox][value="+response[i].worker_id+"]").prop("checked",true);    
});
$("#treatmentModal").modal('show');
} 

相应地更改其他代码。希望有帮助

问题解决了我在这里犯了错误

$(".treatment-btn").click(function()
{
   $("#treatmentModal").modal('show');
   $('.treatment-form')[0].reset();
   $('#treatmentModal #treatment_time').val("");
 }); 

你可以创建一个工作的小提琴或片段来复制这个问题吗?不,我不能,它有大的代码。你不能有更多代码的原因是因为你没有描述你的问题,只是发布了一堆代码。好吧,让我创建它的声音,就像你没有阻止表单的默认操作一样,所以你的页面只是发回-看看这个:是的,它很有帮助,但不能使用它。。我只是发布我的答案,我解决了它。感谢您的回复:
function makeEditHtml(response) {

 $('#treatmentModal #treatment_time 
  option[value='+response[0].treatment_duration+']').attr("selected", "selected"); //i select it again at here after ajax call 
   //i replace it with this below line and it is working ok now
   //$('#treatmentModal #treatment_time option[value='+response[0].treatment_duration+']').prop("selected", true);
 }