简体   繁体   English

在选择框jQuery上动态更改名称属性

[英]dynamically change name attribute on select box jquery

I'm trying to dynamically change the "name" attribute on each select box. 我正在尝试动态更改每个选择框上的“名称”属性。 Here's my scenario: User clicks on first select box option, depending on what the user selected, a new select box appears with the remaining options in it. 这是我的情况:用户单击第一个选择框选项,具体取决于用户选择的内容,将出现一个新的选择框,其中包含其余选项。 User can do this up to 4 times - meaning in total, 4 select boxes can be displayed with first select box containing all options, second select box obtaining 1 less option, and so forth. 用户最多可以执行4次-总共可以显示4个选择框,其中第一个选择框包含所有选项,第二个选择框少包含1个选项,依此类推。 This I have accomplished with some help from others here on stackoverflow. 我已经在其他有关stackoverflow的帮助下完成了此工作。 However, aside from all that, I also have this information of what the user selects for each select box phpmailed to my email account and for this to work, I need to use the "name" attribute on each select box. 但是,除了所有这些,我还获得了有关用户通过phpmail发送到我的电子邮件帐户的每个选择框所选择内容的信息,并且要使其正常工作,我需要在每个选择框上使用“名称”属性。 So my question at hand is, how can I get this to work where each select box that appears has its name attribute dynamically changed (ex: name="guest_al-2" where 2 would change to 3 and so forth once a new select box is dynamically created). 所以我的问题是,如何在出现的每个选择框动态更改其名称属性的情况下使它起作用(例如:name =“ guest_al-2”,其中2更改为3,以此类推,一旦有一个新的选择框,依此类推是动态创建的)。

Here is what I got so far. 这是我到目前为止所得到的。 DEMO As you will note, if you inspect the result part of it, you can see that it generates the name properly, but that name is produced on all new select options that are created. 演示正如您将注意到的,如果您检查其中的结果部分,则可以看到它正确生成了名称,但是该名称是在创建的所有新选择选项上生成的。 How can I get that to dynamically change with what I currently have? 我怎样才能使它随着我现在拥有的东西而动态变化?

JS: JS:

$(document).on('change', '.ad_inquiry_locations', function() {
    $(this).next('select, button').remove();
    var select = $('<select />', {'class' : 'ad_inquiry_locations', value:"", **name:"guest_al-2"**}); // In bold is where I am placing the name attribute for any dynamically created select box
    var option = $('<option />', {text : 'Add a location', disabled : 'disabled'});
    var button = $('<button />', {text : 'Remove Location', click : function() { 
        $(this).prev().remove() 
        if ( $('.ad_inquiry_locations').length == 1 ) $(this).remove();
    }});

    select.append(option, $('option:not(:selected):not(:first)', this).clone(true) );

    $(this).after( select );
    select.after(button);
});

HTML: HTML:

<!-- START OF ADDING LOCATIONS -->
<select id="select1" class="ad_inquiry_locations" value="" name="guest_pl" required>

    <option value="" selected disabled>Select primary location</option>
    <option value="Beloit">Beloit</option>
    <option value="Concordia">Concordia</option>
    <option value="Glen-Elder">Glen Elder</option>
    <option value="Jewell">Jewell</option>

</select>
<!-- END OF ADDING LOCATIONS -->

PHP: - (This part is on a separate file that sends these variables to me via email) PHP:-(此部分位于一个单独的文件中,该文件通过电子邮件将这些变量发送给我)

$primaryLocation = $_POST['guest_pl'];
$addLocation2 = $_POST['guest_al-2'];
$addLocation3 = $_POST['guest_al-3'];
$addLocation4 = $_POST['guest_al-4'];

On option is by adding var boxNumber = 2; On选项是通过添加var boxNumber = 2; outside as a global and increment it every time a new select is appended. 外部作为全局变量,并在每次添加新select时将其递增。 You can seed from any number you want. 您可以从任意数量的种子中获取种子。

    var boxNumber = 2;
    $(document).on('change', '.ad_inquiry_locations', function () {
        $(this).next('select, button').remove();
        var select = $('<select />', {
                'class' : 'ad_inquiry_locations',
                value : "",
                name : "guest_al-" + boxNumber      });
       ....//existing code
       .....
       boxNumber = boxNumber + 1;
        .....
    });


    var button = $('<button />', {
   text: 'Remove Location',
   click: function() {
  $(this).prev().remove()
  if ($('.ad_inquiry_locations').length == 1){ 
  $(this).remove();
  boxNumber = boxNumber-1; //decrements when removed :)
  }
 }
});

Output 输出量 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM