我正在尝试实现this answer中给出的代码。我想进一步简化它,使用closest()找到最近的div的id,并使用它作为位置,但它不工作-我只是在我的文本框中得到“未定义的”。
下面是剧本:
function updateDetails(date) {
var loc = $(this).closest("div").attr("id");
$('#buy-form').animatescroll({scrollSpeed:700,easing:'easeInOutSine'});
document.getElementById('date').value= date;
document.getElementById('venue').value= loc;
$('#resicheck').attr('disabled', true);
}下面是(简化的)标记:
<div id="Philadelphia">
<a href="javascript:updateDetails('20-24 January');">20-24 January</a>
</div>我做错了什么?
发布于 2013-12-02 11:20:19
在中的应用
你必须通过this
<div id="Philadelphia">
<a href="javascript:updateDetails('20-24 January',this);">20-24 January</a>
</div>IN JS
function updateDetails(date,this) {
var loc = $(this).closest("div").attr("id");
$('#buy-form').animatescroll({scrollSpeed:700,easing:'easeInOutSine'});
document.getElementById('date').value= date;
document.getElementById('venue').value= loc;
$('#resicheck').attr('disabled', true);
}和jquery,您喜欢以下非常简单的方式--
现在在html中
<div id="Philadelphia">
<a href="#">20-24 January</a>
</div>In JS
$("#Philadelphia").on("click","a",function(e){
e.preventDefault();
var date=$(this).text();
var loc = $(this).closest("div").attr("id");
$('#buy-form').animatescroll({scrollSpeed:700,easing:'easeInOutSine'});
$('#date').val(date);
$("#venue").val(loc);
$('#resicheck').attr('disabled', true);
});https://stackoverflow.com/questions/20326977
复制相似问题