[英]Make Ajax Post when val on input changes and checkbox is checked
I have 3 input fields: 我有3个输入字段:
Title: <input type="text" value="Testa Ieraksts" id="blog_title"><br>
Check: <input type="checkbox" value="" id="blog_url_change"><br>
URL: <input type="text" value="" id="blog_slug" disabled="disabled"><br>
GOAL: When Checkbox is checked input#blog_slug is set to non-disabled, and then i want on input#blog_title changes send ajax post with var DATA. 目标:当选中复选框时,输入#blog_slug设置为非禁用,然后我想对输入#blog_title进行更改,以var DATA发送ajax帖子。
jQuery(document).ready(function($){
$('input#blog_url_change').click(function(){
$('input#blog_slug').attr('disabled', !$(this).attr('checked'));
var STATUS = $('input#blog_url_change').prop('checked');
if (STATUS === true) {
$('input#blog_title').on('change', function() {
var DOMAIN = window.location.hostname;
var DATA = this.value;
$.ajax({
type: 'POST',
url: "http://" + DOMAIN + "/blog/helper/geturl",
data: 'data=' + DATA,
dataType: 'json',
success: function (response) {
console.log(response.url);
$('input#blog_slug').val(response.url);
},
}); // End Ajax
});
}
});
});
Problem : I need post only when checkbox is checked and title input val changes. 问题 :仅在选中复选框并更改标题输入值时才需要发布。 How to link these mothods together? 如何将这些方法联系在一起?
Thanks For @iCore ;) 感谢@iCore;)
$('input#blog_title').on('change', function() {
$('input#blog_url_change').click(function(){
$('input#blog_slug').attr('disabled', !$(this).attr('checked'));
var STATUS = $('input#blog_url_change').prop('checked');
console.log(STATUS);
if (STATUS === true) {
var DOMAIN = window.location.hostname;
var DATA = $('input#blog_title').val();
$.ajax({
type: 'POST',
url: "http://" + DOMAIN + "/blog/helper/geturl",
data: 'data=' + DATA,
dataType: 'json',
success: function (response) {
console.log(response.url);
$('input#blog_slug').val(response.url);
},
}); // End Ajax
}// End If
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.