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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)