jQuery - 按钮不会做任何事情?
我正在开发一个带有按钮的搜索表单,该按钮最多可在表单中添加 4 个州/城市下拉列表。我试图在每个附加的州/城市集之后添加一个“删除”按钮,最终应该从 DOM 中 .remove()
父 div,但我什至无法让它触发 点击时发出警报
。有什么想法我做错了吗?
这是 HTML:
<div id="stuff">
<div class="holder">
<select class="state">
<option>State</option>
</select>
<span class="city"></span>
</div>
</div>
<button id="addone">ADD ONE</button>
<button id="printit">OUTPUT</button>
<div id="output"></div>
这是 jQuery:
<script type="text/javascript">
<!--
$(document).ready(function() {
$('#addone').click(function() {
var $newRow = $('.holder:first').clone();
$newRow.find('select').val('');
$newRow.find('select.city').hide();
// THIS IS WHERE THE Remove BUTTON IS APPENDED TO THE FORM
$newRow.append('<input name="remove-this" type="button" class="remove-this" value="Remove" />');
if ($('select.state').length < 4) {
$newRow.appendTo('#stuff');
} else {
$newRow.appendTo('#stuff');
$('#addone').attr('disabled','disabled');
}
});
// THIS IS THE PROBLEM AREA
$('.remove-this').click(function() {
alert('hello');
// $(this).parent().remove();
});
$('select.state').live('change',function() {
$(this).next('span.city').find('select[id*="CITIES"]').val('').hide();
$(this).next('span.city').find('select[id="' + $(this).val() + 'CITIES"]').show();
});
$('#printit').click(function() {
var output = '';
$('select.state').each(function() {
output += $(this).val() + ' : ' + $(this).next('select.city').val() + '<br/>';
});
$('#output').html(output);
});
$.ajax({
type: 'GET',
url: 'cities.xml',
dataType: 'xml',
success: function(xml) {
var select = $('select.state');
$(xml).find('STATES').each(function() {
$(this).find('state').each(function() {
var value = $(this).attr('value');
var label = $(this).text();
select.append('<option value="'+ value +'">' + label + '</option>');
});
});
var select = $('span.city');
$(xml).find('STATES').each(function() {
$(this).find('state').each(function() {
var value = $(this).attr('value');
select.append('<select id="'+ value +'CITIES" name="'+ value +'CITIES" class="city"><option>City</option></select>');
});
});
$('#stuff').find('select.city').each(function() {
var select = $(this).attr('id');
$(xml).find(select).each(function() {
$(this).find('city').each(function() {
var value = $(this).text();
var select_j = $('#'+select);
select_j.append('<option value="'+ value +'">' + value + '</option>');
});
});
});
}
});
});
//-->
</script>
I'm developing a search form with a button that adds up to 4 State/City dropdowns to the form. I'm trying to add a Remove button after each additional State/City set that ultimately should .remove()
the parent div from the DOM, but I can't even get it to trigger an alert
when clicked. Any ideas what I'm doing wrong?
Here's a link to the page in development.
Here's the HTML:
<div id="stuff">
<div class="holder">
<select class="state">
<option>State</option>
</select>
<span class="city"></span>
</div>
</div>
<button id="addone">ADD ONE</button>
<button id="printit">OUTPUT</button>
<div id="output"></div>
Here's the jQuery:
<script type="text/javascript">
<!--
$(document).ready(function() {
$('#addone').click(function() {
var $newRow = $('.holder:first').clone();
$newRow.find('select').val('');
$newRow.find('select.city').hide();
// THIS IS WHERE THE Remove BUTTON IS APPENDED TO THE FORM
$newRow.append('<input name="remove-this" type="button" class="remove-this" value="Remove" />');
if ($('select.state').length < 4) {
$newRow.appendTo('#stuff');
} else {
$newRow.appendTo('#stuff');
$('#addone').attr('disabled','disabled');
}
});
// THIS IS THE PROBLEM AREA
$('.remove-this').click(function() {
alert('hello');
// $(this).parent().remove();
});
$('select.state').live('change',function() {
$(this).next('span.city').find('select[id*="CITIES"]').val('').hide();
$(this).next('span.city').find('select[id="' + $(this).val() + 'CITIES"]').show();
});
$('#printit').click(function() {
var output = '';
$('select.state').each(function() {
output += $(this).val() + ' : ' + $(this).next('select.city').val() + '<br/>';
});
$('#output').html(output);
});
$.ajax({
type: 'GET',
url: 'cities.xml',
dataType: 'xml',
success: function(xml) {
var select = $('select.state');
$(xml).find('STATES').each(function() {
$(this).find('state').each(function() {
var value = $(this).attr('value');
var label = $(this).text();
select.append('<option value="'+ value +'">' + label + '</option>');
});
});
var select = $('span.city');
$(xml).find('STATES').each(function() {
$(this).find('state').each(function() {
var value = $(this).attr('value');
select.append('<select id="'+ value +'CITIES" name="'+ value +'CITIES" class="city"><option>City</option></select>');
});
});
$('#stuff').find('select.city').each(function() {
var select = $(this).attr('id');
$(xml).find(select).each(function() {
$(this).find('city').each(function() {
var value = $(this).text();
var select_j = $('#'+select);
select_j.append('<option value="'+ value +'">' + value + '</option>');
});
});
});
}
});
});
//-->
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于您要动态添加 .remove-this,因此请使用 live 函数绑定点击事件。
Since you are adding the .remove-this dynamically, use live function to bind the click event.
应该是:
因为元素被添加到 DOM 中,所以简单的 click() 是不行的。
it should be:
because the element is added to the DOM, so a simple click() won't do.
乍一看,您的 .remove-this 元素似乎是动态创建的。您需要使用 live 或 delegate 来动态绑定点击函数。
At first glance, it looks like your .remove-this element is created dynamically. You'll need to use either live or delegate to bind the click function dynamically.
jQuery .live() 已在 1.9 版本中删除,因此请使用
#happyCoding
jQuery .live() has been removed in version 1.9 onwards, so please use
#happyCoding