简体   繁体   English

用先前保存的元素替换元素(选择框)

[英]Replacing element with a previously saved element (select boxes)

I've got some javascript to change a select box to a text box if an option is selected then change that text box back to a select box if the user hits delete/backspace to the point where they clear the text box. 如果选择了一个选项,我有一些javascript将选择框更改为文本框,然后如果用户点击删除/退格到他们清除文本框的位置,则将该文本框更改回选择框。

http://jsfiddle.net/5e1stz46/6/ http://jsfiddle.net/5e1stz46/6/

<select id='visit'>
    <option value='1'>option1</option>
    <option value='2'>option2</option>
    <option value='3'>option3</option>
    <option value='Other'>Other</option>
</select>

$('#visit').on('change', function () {
        if ((this.value) == 'Other') {
            tmp = this;
            $(this).replaceWith($('<input/>',{'type':'text','id':'visit'})); 
        }
});

$(document).keyup(function(e){
    if( $('#visit').length ) {
        if($('#visit').is(":focus") && $("#visit").val().length == 0){
            $("#visit").replaceWith(tmp);
        }
    }
});

$('#visit').keyup(function(e){
    if(e.keyCode == 46 || e.keyCode == 8) {
        alert('box now empty');
    }
});

When the text box changes back into a select box, all the events it had seem to be lost. 当文本框变回选择框时,它所有的事件似乎都会丢失。

Should I be somehow reading the event listeners when the select box comes back or should I be saving the select box differently to start with? 当选择框返回时,我应该以某种方式阅读事件监听器,还是应该以不同的方式保存选择框?

The events got removed because you're removing associated Html element from the DOM. 事件已被删除,因为您正在从DOM中删除关联的Html元素。 You can have both select and input and play with .hide() and .show to visualize the correct element and the events will work fine. 你可以同时selectinput并使用.show .hide().show来显示正确的元素,事件将正常工作。


Another approach could be to use event delegation . 另一种方法可能是使用事件委托

Changed Html: 更改了Html:

<div id="wrapper">
    <select id='visit'>
        <option value='1'>option1</option>
        <option value='2'>option2</option>
        <option value='3'>option3</option>
        <option value='Other'>Other</option>
    </select>
</div>

Changed JavaScript: 改变了JavaScript:

$('#wrapper').on('change', '#visit', function () {
    if ((this.value) == 'Other') {
        tmp = this;
        $(this).replaceWith($('<input/>',{'type':'text','id':'visit'})); 
    }
});

[ ... ]

Rest of the code remains the same. 其余代码保持不变。 Here is an updated JsFiddle Demo . 这是一个更新的JsFiddle演示

Once you remove the element from DOM, all its handlers are gone as well. 从DOM中删除元素后,其所有处理程序也都消失了。
One solution would be to always keep the element, but just hide it from the user's view. 一种解决方案是始终保留元素,但只是将其隐藏在用户的视图中。

 $('select[name=visit]').on('change', function () { if (this.value == 'Other') { $(this).addClass('hide'); $('input.hide').removeClass('hide'); } }); $(document).keyup(function (e) { var $elem = $('input[name=visit]'); if ($elem.length) { if ($elem.is(":focus") && $elem.val().length == 0) { $elem.addClass('hide'); $("select[name=visit]").removeClass('hide'); } } }); $('input[name=visit]').keyup(function (e) { if (e.keyCode == 46 || e.keyCode == 8) { alert('box now empty'); } }); 
 .hide { display: none; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <select name="visit"> <option value='1'>option1</option> <option value='2'>option2</option> <option value='3'>option3</option> <option value='Other'>Other</option> </select> <input type="text" name="visit" class="hide" /> 

Also as there could be only one element with the unique id I always avoid using id attribute in dynamic HTML. 另外因为只有一个元素具有唯一id所以我总是避免在动态HTML中使用id属性。

Second solution 二解决方案
Add a parent div wrapper and attached the handler to it as shown below: 添加父div包装器并将处理程序附加到它,如下所示:

 $('#wrapper').on('change', '#visit', function () { if ((this.value) == 'Other') { tmp = this; $(this).replaceWith($('<input/>', { 'type': 'text', 'id': 'visit' })); } }); $(document).keyup(function(e){ if( $('#visit').length ) { if($('#visit').is(":focus") && $("#visit").val().length == 0){ $("#visit").replaceWith(tmp); } } }); $('#visit').keyup(function(e){ if(e.keyCode == 46 || e.keyCode == 8) { alert('box now empty'); } }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script> <div id="wrapper"> <select id='visit'> <option value='1'>option1</option> <option value='2'>option2</option> <option value='3'>option3</option> <option value='Other'>Other</option> </select> </div> 

JSFIDDLE 的jsfiddle

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

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