dongpu9481 2014-09-17 09:22
浏览 16
已采纳

当field1在表单中更改时更改field2

I have a form, which it ask for the field1 a serial number. Then in the 2nd field, it will ask the model. But I can find the model with the serial number. Its the 3 & 4th numbers of the serial. So instead of asking the model number, I wanted to put directly the model without the user intervention.

I want the field2 directly changed when the field1 has been updated, not after the post of the form.

So first, is it possible only with php ? My knowledge in jQuery, Ajax and JS are very limited.

Here's how I'm doing now :

        <div class="col-xs-6">
            <div class="form-group">
            <label for="no_serie_produit" class="label col-xs-3">N° Série Produit</label>
                <div class="col-xs-10">
                    <input type="text"  name="no_serie_produit" id="no_serie_produit" placeholder="N° Série de ... (Obligatoire) ">
                </div>
            </div>
            <div class="form-group">
                <label for="model_produit" class="label col-xs-3">Modele Produit</label>
                    <div class="col-xs-10">
                        <input type="text"  name="model_produit" id="model_produit" placeholder="Modèle ... (Obligatoire)">
                    </div>
            </div>
        </div>

2nd : If I really need to use something like JS or jQuery, can you suggest me the most easy one to learn.

EDIT

Example :

If I put in Serial n° : 14020001, I want in the field2 : TMSA4-NET LV

  • 写回答

1条回答 默认 最新

  • du0531 2014-09-17 09:30
    关注

    Listen for the input event on #no_serie_produit, then set the value of #model_produit accordingly:

    $('#no_serie_produit').on('input', function() {
        // don't do anything before 4 digits are inputted
        if (this.value.length < 4) return;
    
        // .slice()'s start is inclusive, end is exclusive
        var modelId = this.value.slice(3, 5);
    
        // do the Ajax call with $.ajax or the shorthand $.post/$.get methods
        $.get('path/to/getModelName.php', { modelId: modelId }, function(modelName) {
            // received the model name in the Ajax response, update the field
            $('#model_produit').val(modelName);
        });
    });
    

    This will send an Ajax request to path/to/getModelName.php (update the path/filename accordingly), in this PHP file you will be able to access the model ID through $_GET['modelId']. Then you can query the database with it and echo the name. The echoed value will be returned as the response of the Ajax call, that is, the $.get's callback's modelName parameter in this case.

    Some reading reference:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题