我有2個jQuery事件發生在form
內部。 form
內有一個select
元素:如何在函數-2中使用jquery函數-1變量值?
事件1處於選定更改狀態。它的存儲選擇的選項值的變量:
$('#sm_name').change(function(){
var option_value = $('#sm_name option:selected').val();
console.log(option_value);
});
事件2是在表單提交使用$.ajax()
:
$("#fb_form").on('submit', function (e) {
e.preventDefault();
$("#message").empty();
$("#loading").show();
$.ajax({
url: "submit.php",
type: "POST", // Type of request to be send, called as method
data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
contentType: false, // The content type used when sending data to the server.
cache: false, // To unable request pages to be cached
processData: false, // To send DOMDocument or non processed data file it is set to false
success: function (data) { // A function to be called if request succeeds
}
});
});
我怎樣才能動態地改變AJAX URL從select
下拉列表中每個選定的價值?事情是這樣的:
url: "submit.php?id=" + option_value,
網址: 「submit.php ID =?」 + $( '#sm_name' ).val() – Majid
declare var option_value;全球。或使用url:「submit.php?id =」+ $('#sm_name option:selected')。val() –
@DeepakSharma雖然它會起作用,但應避免使用全局變量 –