运行AJAX脚本不工作?

用户在ind文件中输入id=2时Ajax会运行。

HTML:

<input id="2" type="text" onkeyup="posttitulo(this.value)" />

SCRIPT:

function posttitulo(value){
$.post("getdata/posttitulo.php",{partialStates:value});
}

代码工作得很好...问题是,我的javascript函数在单击另一个输入id=1的拷贝值,在本例中id=2有值而不键入,函数postTitulo(Value)也不工作。

所以如果需要执行Ajax:

1)用户在keyup=“postTitulo(this.value)”上写东西;

2)将id=1的值复制到id=2。

希望我解释清楚了......先提前谢谢你!

create an event listener change() on id=2:

$(document).ready(function(){
 $("#2").change(function(){
  var value=$("#2").val();
  $.post("getdata/posttitulo.php",{partialStates:value});

 });
});

Do $('#2').keyup(); after you copied a value in the function, which fires onclick.

You could go with firing the event on the input or recycle the function you already declared. When the button is clicked read the value of the id2 Element and call the posttitulo function with the value as argument.

function posttitulo(value){
    console.log('AJAX POST:', value);
}

function buttonClicked() {
    var value = document.getElementById('id2').value;
    posttitulo(value);
}

Fiddle

May be this will work in your case.

<input id="1" type="text" onclick="yourreplacefunction(this.value,2)" />

$(document).on("change keyup", "input", function() {
    $.post("getdata/posttitulo.php",{partialStates:value},function(response){
        //Do with your response
    });
});

function yourreplacefunction(value,toid) {
       $('#'+toid).val(value);
}