在选择选项的onchange function()中传递多个名称

时间:2018-06-26 11:33:04

标签: javascript php jquery ajax

我正在尝试在选择选项中传递多个文本。我在这里尝试了很多示例,但是没有用。我哪里错了? 下面是我的代码

<select name="region" id="region" class="region" onchange="change_pack();">
        <option selected="selected" value="'Base','abs','Ram'">North</option>
        <option value="'Base','noabs','Sita'">South</option>
</select>

下面是我的脚本

function change_pack(pack, table, name){
    $.ajax({
      type: "POST",
      url: "extra/plan.php",
      data: "name="+name+"&tble="+table+"&pack="+pack,
      cache: false,
      success: function(response){
        //alert(response);return false;
        $("#plan").html(response);
      }
   });
}

5 个答案:

答案 0 :(得分:1)

输入值通常是字符串,因此您需要将字符串拆分为值

<select name="region" id="region" class="region" onchange="change_pack(this.value);">
        <option selected="selected" value="'Base','abs','Ram'">North</option>
        <option value="'Base','noabs','Sita'">South</option>
</select>

js:

function change_pack(data)
{
    var [pack, table, name] = data.split(",");
    $.ajax({
    type: "POST",
    url: "extra/plan.php",
    data: "name="+name+"&tble="+table+"&pack="+pack,
    cache: false,
    success: function(response)
        {
            //alert(response);return false;
            $("#plan").html(response);
        }
        });
}

答案 1 :(得分:0)

您可以在ajax中传递逗号分隔的值:

HTML:

<select name="region" id="region" class="region" onchange="change_pack(this);">
        <option selected="selected" value="'Base','abs','Ram'">North</option>
        <option value="'Base','noabs','Sita'">South</option>
</select>

JS

function change_pack(data)
{ 

  var selectedval = $(data).val();
  var [pack, table, name] = selectedval.split(",");
    $.ajax({
    type: "POST",
    url: "extra/plan.php",
    data: "name="+name+"&tble="+table+"&pack="+pack,
    cache: false,
    success: function(response)
        {
            //alert(response);return false;
            $("#plan").html(response);
        }
  });
}

演示:https://codepen.io/creativedev/pen/aKaVBX

答案 2 :(得分:0)

option可以分配一个值。您可以将其设置为可以存储额外数据的其他对象的键:

<select name="region" id="region" class="region" onchange="change_pack();">
  <option selected="selected" value="north">North</option>
  <option value="south">South</option>
</select>

JS:

var values = {
  north: {
    name: 'Base',
    table: 'abs',
    pack: 'Ram'
  },
  ...
}

function change_pack(value) {
  var data = values[value];
  // if `north` will be selected, data will contain:
  // { name: 'Base', table: 'abs', pack: 'Ram' }
}

答案 3 :(得分:0)

您可以尝试使用ajax的表单序列化获取所有表单数据,并将其传递给后端“ https://api.jquery.com/serialize/

https://jsfiddle.net/sjhu1nt5/13/

In [81]: for n, line in enumerate(f):
    ...:     buffer[n%3] = line
    ...:     a, b, c = b, c, a
    ...:     if (n>1 and
    ...:         buffer[a].startswith('dn') and
    ...:         buffer[b].startswith('AccessFTPexpire') and
    ...:         buffer[c].startswith('ftpUser')) :
    ...:         print(buffer[a], buffer[b], buffer[c], sep='', end='')
dn: uid=case_101,ou=ftpusers,ou=applications,o=regg.com
AccessFTPexpire: 05/03/2017
ftpUser: T
dn: uid=case_201,ou=ftpusers,ou=applications,o=regg.com
AccessFTPexpire: 05/03/2017
ftpUser: Y

答案 4 :(得分:0)

 <select name="region" id="region" class="region" onchange="change_pack(this.value);">
        <option selected="selected" value="'Base','abs','Ram'">North</option>
        <option value="'Base','noabs','Sita'">South</option>
</select>
function change_pack(data)
{   

    var [pack, table, name] = data.toString().split(",");

    $.ajax({
    type: "POST",
    url: "extra/plan.php",
    data: "name="+name+"&tble="+table+"&pack="+pack,
    cache: false,
    success: function(response)
        {
            //alert(response);return false;
            $("#plan").html(response);
        }
        });
}