2017-06-05 132 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); 
}