0

我有一个简单的表单,我正在使用它,并且我正在尝试在单击命令按钮时更新文本框值。命令按钮称为 btnVerifyLocation,文本框称为 txtGeoLocation。我尝试在 Javascript 中执行以下操作:

我的代码如下:

<script type="text/javascript" id="testing">

$("btnVerifyLocation").click(function ()
{
     $("input[name*='txtGeoLocation']").val("testing");
});

</script>

但是,当我单击按钮时,什么也没有发生。

4

2 回答 2

1

A)你在'btnVerifyLocation'中缺少一个#(我假设这是它的ID,否则如果它是一个类,那么使用'.btnVerifyLocation'

B)其次,这应该在 a 中$(document).ready(),否则您试图将点击处理程序绑定到尚未呈现的 DOM 元素。

代码应如下所示:

$(document).ready(function() {
    $('#btnVerifyLocation').click(function(e) {
        e.preventDefault(); // In case this is in a form, don't submit the form
        // The * says "look for an input with a name LIKE txtGeoLocation, 
        // not sure if you want that or not
        $('input[name*="txtGeoLocation"]').val('testing'); 
    });
});
于 2013-05-01T23:58:42.810 回答
1

jQuery 的选择器函数使用 CSS 选择器语法,因此要使用 ID 标识对象,您需要在 ID 前面加上#:

$("#btnVerifyLocation").click(function () {
    $("input[name*='txtGeoLocation']").val("testing");
});

也以防万一:您确实包含了 jQuery,对吗?

于 2013-05-01T23:58:51.950 回答