我有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虽然它会起作用,但应避免使用全局变量 –