我有一个 AJAX 功能,当我在我的 PC 上时,它工作得很好。但是当我切换到 Safari(移动设备)时,只有单选框会触发 AJAX。怎么会这样?
HTML:
<input type="radio" id="q1r1" name="q1" value="Awesome" onchange="GrabData(this)">
<input type="radio" id="q1r3" name="q1" value="Awful" onchange="GrabData(this)">
<div class="comment"><textarea name='q1comment' id='comment' maxlength="400" placeholder="Add a comment (max 400 characters)" onchange="GrabC(this)"></textarea>
AJAX
//AJAX question 1.
function GrabData(Passeddata){
var radioValue = Passeddata.value;
var URL = "question1.php?q1=" + radioValue + "&teamid=" + <?php echo $teamid; ?>;
$.ajax( {
url : URL,
type : "GET",
dataType: 'json',
success : function(data) {
if (data == "") {
alert("data is empty");
} else {
console.log('got your data back sir');
}
}
});
};
//AJAX for comment question 1.
function GrabC(PassedComment){
var radioValue = PassedComment.value;
var URL = "question1.php?comment1=" + radioValue + "&teamid=" + <?php echo $teamid; ?>;
$.ajax( {
url : URL,
type : "GET",
dataType: 'json',
success : function(data) {
if (data == "") {
alert("data is empty");
} else {
console.log('got your data back sir');
}
}
});
};
同样,在我的 PC 上工作正常,textarea onchange 似乎不适用于移动设备上的 Safari。不明白为什么!
最佳答案
尝试将 keyup
事件与 change
一起使用;当您模糊、聚焦(不确定这一点)元素时,change
会触发,但 keyup
实际上会在用户键入/点击按键时监听更改。
$('input[type="text"]').on('change keyup', function(){
$('#console').text($('#console').text()+ "\r\n"+ this.value);
});
$('textarea').on('change keyup', function(){
$('#console').text($('#console').text()+ "\r\n"+ this.value);
/** Send to AJAX
GrabC(this);
GrabData(this);
**/
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="text"/>
<textarea row="4"></textarea>
<pre id="console"></pre>
关于javascript - 用于文本区域的 Onchange AJAX 在 Safari 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34666875/