0

我有一个选择菜单,我想动态添加更多选择输入。我找到了这个解决方案:https ://gist.github.com/snipe/1925906 ,它在没有 jQuery Mobile 的情况下工作得很好。但是 jQuery Mobile 会修改元素并添加自己的代码。所以在选择菜单本身之前,jQM 添加了这样的元素(还有一些,但不是那么重要):

<span class='ui-btn-inner'><span class='ui-btn-text'><span>Option Name</span></span><span class='ui-icon ui-icon-arrow-d ui-icon-shadow'>&nbsp;</span></span>

这个“选项名称”已经在这里,当我调用使用“clone()”命令的函数时,它也会克隆这个“选项名称”。因此,第一个选择菜单可以正常工作,并且它的选项名称发生了变化,但是在克隆的菜单中,即使我选择了另一个选项,它也保持不变。

更新:这是我的代码(抱歉在几个地方使用俄语)。带有选择和文本输入的 HTML:

<fieldset class='ui-grid-a'>
<div id='inputphone1' class='clonedInputPhone'>
    <div class='ui-block-a' style='max-width: 200px;'>
    <select name='phone_type[]' data-mini='true'>
        <option value="mobile">Мобильный</option>
        <option value="home">Домашний</option>
        <option value="work">Рабочий</option>
    </select> 
    </div>
</div> <!-- inputphone1 -->
<div id='textinputphone1' class='textclonedInputPhone'>
    <div class='ui-block-b'>
    <input type='text' name='phone_number[]' data-mini='true' />
    </div>
</div> <!-- textinputphone1 -->
</fieldset> 

<fieldset class="ui-grid-a">
    <div class="ui-block-a" style="max-width: 200px;">
    </div>
    <div class="ui-block-b">
       <input id="btnAddPhone" type="button" data-icon="plus" data-iconpos="notext" data-inline="true" data-mini="true" onClick="addInput('phone_type', 'phone_number', 'clonedInputPhone', 'inputphone', 'btnAddPhone', 'btnDelPhone', '7', '0');" />
       <input id="btnDelPhone" type="button" data-icon="minus" data-iconpos="notext" data-inline="true" data-mini="true" disabled="disabled" onClick="removeInput('clonedInputPhone', 'inputphone', 'btnAddPhone', 'btnDelPhone');" />
    </div>
</fieldset> 

和js函数:

function addInput(selectName, textName, clonedInputName, inputName, btnAddName, btnDelName, limit, numstart){

// how many "duplicatable" input fields we currently have
var num = $('.' + clonedInputName).length;  

$('#' + btnDelName).removeAttr('disabled').button('enable');

// the numeric ID of the new input field being added
var newNum  = new Number(num + 1);

var newSelect = $('#' + inputName + num ).clone().attr('id', inputName + newNum);                                 
$('#text' + inputName + num).after(newSelect);  

var newText = document.createElement('div');
newText.id = 'text' + inputName + newNum;
newText.className = 'text' + clonedInputName;   
newText.innerHTML = "<div class='ui-block-b'><div class='ui-input-text ui-shadow-inset ui-corner-all ui-btn-shadow ui-body-c ui-mini'><input type='text' name='" + textName + "[]' data-mini='true' class='ui-input-text ui-body-c'></div></div>";
$(newSelect).after(newText);

if (newNum == limit - numstart)  {
    $('#' + btnAddName ).prop('disabled', 'disabled').button('disable');
} 
};

function removeInput(clonedInputName, inputName, btnAddName, btnDelName){

// how many "duplicatable" input fields we currently have           
var num = $('.' + clonedInputName).length;  

// remove the last element  
$('#' + inputName + num ).remove();  
$('#text' + inputName + num ).remove();     

// enable the "add" button, since we've removed one             
$('#' + btnAddName).removeAttr('disabled').button('enable');    

// if only one element remains, disable the "remove" button     
if ( num - 1 == 1)
$('#' + btnDelName ).prop('disabled', 'disabled').button('disable');
};

因为我不能上传截图,这里是:http ://d.pr/i/choo 第二个选择菜单中的标签“Мобильный”没有改变,因为它写在克隆的“span”中。

任何想法如何解决这个问题?谢谢!

4

1 回答 1

0

我找到了一种方法!(一种可怕的方式,可能是,但它有效)。如果有人能提出更优雅的方式,我会很高兴和感激。但我会发布我的解决方案,以防万一。

这次我没有使用“clone()”,我手动创建了 jQuery Mobile 通常添加的所有元素。然后,当我选择另一个选项时,我使用了在跨度中动态更改“选项名称”的函数。这个函数可以在 looooong “newdiv.innerHTML” 部分的末尾找到。

这是我的新 HTML/PHP 代码(我还优化了一些传递给函数的参数):

<div id='input_phone1' class='cloned_input_phone'>
<fieldset class='ui-grid-a'> 
    <div class='ui-block-a' style='max-width: 200px;'>
        <select name='phone_type[]' data-mini='true'>
        <?php
        $selectOptionsPhone = "<option value=\'mobile\'>Мобильный</option><option value=\'home\'>Домашний</option><option value=\'work\'>Рабочий</option>";  
        echo $selectOptionsPhone;
        ?>
        </select> 
    </div>
    <div class='ui-block-b'>
        <input type='text' name='phone_value[]' data-mini='true' />
    </div>
</fieldset> 
</div> <!-- input_phone1 -->

<fieldset class="ui-grid-a">
    <div class="ui-block-a" style="max-width: 200px;">
    </div>
    <div class="ui-block-b">
        <?php
        echo "<input id='btn_add_phone' type='button' data-icon='plus' data-iconpos='notext' data-inline='true' data-mini='true' onClick=\"addInput('phone', 'Мобильный', '".$selectOptionsPhone."', '".$phone_max."', '0');\" />";
        ?>
        <input id="btn_del_phone" type="button" data-icon="minus" data-iconpos="notext" data-inline="true" data-mini="true" disabled="disabled" onClick="removeInput('phone');" />
    </div>
</fieldset> 

还有我的 JS 函数:

function addInput(valueName, selectDefault, selectOptions, limit, startNum){

var inputName = "input_" + valueName;
var clonedInputName = "cloned_input_" + valueName;
var selectName = valueName + "_type[]";
var textName = valueName + "_value[]";
var btnAddName = "btn_add_" + valueName;
var btnDelName = "btn_del_" + valueName;

// how many "duplicatable" input fields we currently have
var num = $('.' + clonedInputName).length;  

$('#' + btnDelName).removeAttr('disabled').button('enable');

// the numeric ID of the new input field being added
var newNum  = new Number(num + 1);

var newdiv = document.createElement('div');
newdiv.id = inputName + newNum;
newdiv.className = clonedInputName; 
newdiv.innerHTML = "<fieldset class='ui-grid-a'><div class='ui-block-a' style='max-width: 200px;'><div class='ui-select'><div data-corners='true' data-shadow='true' data-iconshadow='true' data-wrapperels='span' data-icon='arrow-d' data-iconpos='right' data-theme='c' data-mini='true' class='ui-btn ui-shadow ui-btn-corner-all ui-mini ui-btn-icon-right ui-btn-up-c'><span class='ui-btn-inner'><span class='ui-btn-text'><span class='update" + valueName + num + "'>" + selectDefault + "</span></span><span class='ui-icon ui-icon-arrow-d ui-icon-shadow'>&nbsp;</span></span><select name='" + selectName + "' id='choose" + valueName + num + "' data-mini='true' data-role='none'>" + selectOptions + "</select></div></div></div><div class='ui-block-b'><div class='ui-input-text ui-shadow-inset ui-corner-all ui-btn-shadow ui-body-c ui-mini'><input type='text' name='" + textName + "' data-mini='true' class='ui-input-text ui-body-c'></div></div></fieldset><script type='text/javascript'>$('#choose" + valueName + num + "').change(function(event) {$('span.update" + valueName + num + "').html($(\"#choose" + valueName + num + " option:selected\").text());}); </script>";                                

$('#' + inputName + num).after(newdiv);     

if (newNum == limit - startNum)  {
    $('#' + btnAddName ).prop('disabled', 'disabled').button('disable');
    } 
};

function removeInput(valueName){

var inputName = "input_" + valueName;
var clonedInputName = "cloned_input_" + valueName;
var btnAddName = "btn_add_" + valueName;
var btnDelName = "btn_del_" + valueName;

// how many "duplicatable" input fields we currently have           
var num = $('.' + clonedInputName).length;  

// remove the last element  
$('#' + inputName + num ).remove();  
$('#text' + inputName + num ).remove();     

// enable the "add" button, since we've removed one             
$('#' + btnAddName).removeAttr('disabled').button('enable');    

// if only one element remains, disable the "remove" button

if ( num == 2)
$('#' + btnDelName ).prop('disabled', 'disabled').button('disable');
};

可能这将有助于下次有人。

于 2013-04-20T12:28:09.367 回答