使用ajax函数更改Submit-Button,以便立即对输入作出反应


Change the Submit-Button with AJAX-function for instantly reacting for an input

我想改变从submit-Button到动态ajax函数获得这个表单中输入值的答案的可能性(为数字提供俄语单词),以立即获得答案。

谢谢!!

在@user1978142的帮助下,它可以完美地工作。但现在我在寻找一个解决方案,添加一个播放按钮,用谷歌TTS(文本到语音)拼写单词。我为谷歌TTS找到了一个解决方案,但它只适用于谷歌Chrome。

<?  
    $ru = new NumberFormatter("ru", NumberFormatter::SPELLOUT);     
?>
<form method="get" action="index.php">
    Zahl: 
    <input name="zahl" type="text" size="15" maxlength="15">
    <input type="submit" value="Anzeigen">
</form> 
<?
    if (is_numeric($_REQUEST['zahl'])) echo $ru->format($_REQUEST['zahl']);
?> 

由于jQuery是标记的,并且已经安装了NumberFormatter,所以您只需要一个简单的$.ajax。也要注意编码(我认为ru是指俄语)。考虑这个例子:

你的<<p> strong> index . php
<form method="POST" action="index.php">
    <label for="zahl">Zahl:</label> <br/>
    <input id="zahl" name="zahl" type="number" size="15" maxlength="15"><br/><br/>
    <div id="results" style="width: 400px;"></div> <!-- results appear here -->
</form>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
    $('#zahl').on('keyup', function(){
            // ajax request server
        $.ajax({ url: 'number_process.php', type: 'POST', dataType: 'text', data: {value: $(this).val()},
            success: function(response) {
                $('#results').text(response); // append to result div
            }
        });
    });
});
</script>

number_process.php (样品名称)

创建处理ajax请求的php文件。这将处理它并返回您的值。

<?php
if(isset($_POST['value'])) {
    $ru = new NumberFormatter("ru", NumberFormatter::SPELLOUT);
    // normal processing
    $value = $ru->format($_POST['value']);
    echo mb_convert_encoding($value, 'UTF-8', 'auto'); // russian characters
    exit;
}
?>