jQuery 选择、.change 和 .click
我的脚本有一点问题。
目标:我有一个带有几个选项的 select
,如果您选择值为“4”的选项,选择将删除,在这个地方我需要插入 input[type=text] 和单词“remove”跨度标签。现在,当您单击此跨度时,之前插入的输入和跨度将被删除,在这个地方我需要放置之前删除的选择,
我希望它有意义:-)
这里是我的 id=f9: 表单内的 html 代码
<label class="mandatory" for="ctrl_54">Title</label>
<select class="select mandatory" id="ctrl_54" name="title">
<option value="1">Books</option>
<option value="2">Movies</option>
<option value="3">Events</option>
<option value="4">other...</option>
</select>
和我的 js:
function be_forms() {
var myselect = $('form#f9 select#ctrl_54');
if ($('form#f9').length) {
$('form#f9 select#ctrl_54').change(function() {
if ($(this).val() == 4) {
$('form#f9 select#ctrl_54').remove();
$('<input type="text" value="" class="text innyinput mandatory" id="ctrl_54" name="title" /><span class="remove">remove</span>').insertAfter('form#f9 label[for=ctrl_54]');
$("form#f9 span.remove").click(function(){
$('form#f9 input#ctrl_54').remove();
$('form#f9 span.remove').remove();
$(myselect).insertAfter('form#f9 label[for=ctrl_54]');
});
}
});
}
}
现在几乎一切正常。我的意思是当我选择值为 4 的选项时 - 选择被删除并出现输入和跨度。当我单击此跨度时,我的选择再次出现,并且输入和跨度被删除。 但现在如果我在选择选项中再次选择值 4 - 什么也不会发生。我想再做一次 - 删除选择、插入输入和跨度等等...
抱歉我的英语我不是母语人士。
I have a little problem with my script.
The goal: I have a select
with a few options and if you choose option with value '4' select will remove and in this place I need insert input[type=text] and word "remove" in span tags. And now when you click at this span, previously inserted input and span will remove and in this place I need place my previously removed select
I hope it's make sense :-)
here my html code inside form with id=f9:
<label class="mandatory" for="ctrl_54">Title</label>
<select class="select mandatory" id="ctrl_54" name="title">
<option value="1">Books</option>
<option value="2">Movies</option>
<option value="3">Events</option>
<option value="4">other...</option>
</select>
and my js:
function be_forms() {
var myselect = $('form#f9 select#ctrl_54');
if ($('form#f9').length) {
$('form#f9 select#ctrl_54').change(function() {
if ($(this).val() == 4) {
$('form#f9 select#ctrl_54').remove();
$('<input type="text" value="" class="text innyinput mandatory" id="ctrl_54" name="title" /><span class="remove">remove</span>').insertAfter('form#f9 label[for=ctrl_54]');
$("form#f9 span.remove").click(function(){
$('form#f9 input#ctrl_54').remove();
$('form#f9 span.remove').remove();
$(myselect).insertAfter('form#f9 label[for=ctrl_54]');
});
}
});
}
}
And now almost everythings works. I mean when I choose option with value 4 - select is remove and appear input and span. When I click at this span my select appear again and input and span are removed.
But now if I choose again in my select option with value 4 - nothing happens. And I want do it again - remove select, insert input and span and so on...
Sorry for my english I not native speaker.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从 jQuery 1.7 开始,
.live()
方法已被弃用。使用.on()
附加事件处理程序。旧版本 jQuery 的用户应优先使用.delegate()
而不是.live()
。http://api.jquery.com/live/
As of jQuery 1.7, the
.live()
method is deprecated. Use.on()
to attach event handlers. Users of older versions of jQuery should use.delegate()
in preference to.live()
.http://api.jquery.com/live/
您必须使用
.live()
这里。You will have to use
.live()
here.