【问题标题】:Jquery data split phpjquery数据拆分php
【发布时间】:2014-05-13 16:42:07
【问题描述】:

我有这个array = var1;var2;var3;var4;var5;var6;var6

我想用 jQuery 将其拆分并放入不同的文本框 (txt1)、(txt2)...等, 但我找不到正确的代码。

<script type="text/javascript">
$(document).ready(function() {
    $("#cb_motor").change(function() {
        $("#cb_motor option:selected").each(function() {
           cb_motor = $('#cb_motor').val();
            $.post("http://localhost/sag_app/index.php/motor/motor/buscar_id/", {
                cb_motor : cb_motor
            }, function(data) {
              valores  = data.split(';');                       
            });
        });
    })
});
</script>   

【问题讨论】:

  • 请也提供相关的HTML标记,txt1,txt2等???是ID属性还是什么?

标签: javascript jquery split


【解决方案1】:

如果txt1, txt2 是输入 ID 并按照 DOM 顺序设置,您可以使用:

$("[id^=txt]").val(function(i){
    return valores[i];
});

【讨论】:

    【解决方案2】:

    假设cb_motor是一个没有multiple的select,代码可以简化

    $(function() {
      $("#cb_motor").change(function() {
        $.post("http://localhost/sag_app/index.php/motor/motor/buscar_id/", {
          cb_motor : $(this).val()
        }, function(data) {
          var valores  = data.split(';');         
          $('[name="txt_num_serie_motor"]').each(function(i) {
            $(this).val(valores[i]);
          });
        });
      });
    });
    

    如果它是多个,那么你不能使用每个选择的值进行 Ajax,因为你需要等待每个调用返回

    【讨论】:

    • 那是我的文本框,我有吗添加一些东西?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-12
    • 1970-01-01
    相关资源
    最近更新 更多