[英]Jquery change isn't working after ajax post value received
I have here Jquery code with ajax/json. 我这里有带有ajax / json的Jquery代码。 First i will discuss the flow.
首先,我将讨论流程。 I have 3 textboxes, my
item
textbox pass it's value to auto-complete.php through ajax to get it's details. 我有3个文本框,我的
item
文本框通过ajax将其值传递给auto-complete.php以获得详细信息。 The return value is post or place into mode
textbox. 返回值是post或放在
mode
文本框中。 And if the post value is 1 or 2 the number
textbox should change is css into display:none
. 并且如果柱值是1或2的
number
文本框应该改变是CSS成display:none
。 But the problem this not working, I set the number
textbox into readonly. 但是这个问题不起作用,我将
number
文本框设置为只读。 The change function is working when I change directly the value of number
textbox. 当我直接更改
number
文本框的值时,更改功能起作用。 Why isn't working when the value is post value? 值是后值时为什么不起作用?
<script>
$(document).ready(function() {
$("#number").css("display","none");
$(document).on('change','#mode',function(){
if($("#mode").val() =="1"){
$("#number").css("display","");
} else if($("#mode").val() =="2"){
$("#number").css("display","");
} else {
$("#number").css("display","none");
}
return true;
});
});
</script>
<input name="item" id="item" type="text"/>
<input name="mode" id="mode" type="text"/>
<input name="number" id="number" type="text" readonly />
<script type="text/javascript">
$(document).ready(function()
{
$('#item').change(function()
{
var item= $("#item").val();
$.ajax({
type: "POST",
url: "autocomplete-ajax.php",
data :"item="+item,
dataType:'json',
type:'POST',
success:function(data){
var mode=data.mode;
//send to element ID
$('#mode').val(mode);
}
});
return false;
});
});
</script>
When you change the value using JavaScript it won't trigger events. 当您使用JavaScript更改值时,它不会触发事件。
A simple fix is to trigger the event yourself after you update the value. 一个简单的解决方法是在更新值之后自行触发事件。
success:function(data){
var mode=data.mode;
//send to element ID
$('#mode').val(mode).change();
/* OR */
$('#mode').val(mode).trigger('change');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.