2017-06-05 59 views
0
$(".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'); 
} 

//当ajax请求触发时,我会调用这个函数,我以相同的形式追加数据,但选择框的值没有被选中。它显示正确的萤火虫,但不显示在屏幕上。第一次工作正常选择选项2次jquery不能正常工作

+0

错误请您创建一个工作小提琴或片段复制的问题? –

+0

不,我不能让它有大代码 –

+0

他的意思是[创建一个jsfiddle](https://jsfiddle.net/)。你不能拥有更多代码的原因是因为你没有描述你的问题,而只是发布了一段代码。 – JiFus

回答

1

将您的点击功能代码更改为下方。要重置选择,您可以简单地将其值设置为空白。

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

相应地更改其他代码。希望能帮助到你!

+0

是的,它是有益的,但不与它合作..我只是发布我的答案我解决了它。感谢您的答复 :) –

0

问题解决了我在这里做

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); 
}