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

时间:2017-06-05 12:57:03

标签: javascript jquery ajax

$(".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请求触发我以相同的形式附加数据但是选择未选中的框值时调用。它在firebug中显示正确但不在屏幕上显示。第一次工作正常

2 个答案:

答案 0 :(得分:1)

将您的点击功能代码更改为以下内容。要重置选择,您只需将其值设置为空白即可。

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

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

答案 1 :(得分: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);
 }