将ASP.Net控件传递到JavaScript函数中
我已经编写了一个javascript函数,我想在文本框、dropdownlist和整数中使用它。在函数中,我需要检查文本字符串的长度和DDL的值。功能如下:将ASP.Net控件传递到JavaScript函数中,javascript,asp.net,controls,Javascript,Asp.net,Controls,我已经编写了一个javascript函数,我想在文本框、dropdownlist和整数中使用它。在函数中,我需要检查文本字符串的长度和DDL的值。功能如下: function CheckSearch(name, code, iMinSize) { if (name.value.length < iMinSize && code.value === '') { alert('You must enter ' + iMinSize + ' letters or more
function CheckSearch(name, code, iMinSize) {
if (name.value.length < iMinSize && code.value === '') {
alert('You must enter ' + iMinSize + ' letters or more when searching.');
name.focus();
return false;
}
else {
return true;
}
函数检查搜索(名称、代码、iMinSize){
if(name.value.length
}
我认为这个函数还可以,但我似乎无法从ASP.NET中的按钮控件调用它
这是我的按钮标签:
<asp:Button ID="buttonSearch" runat="server" Text="Search" OnClick="buttonSearch_Click"
OnClientClick="javascript:return CheckSearch('textBoxSearch','comboCodes', 3);" />
单击按钮时,我收到一个对象预期错误
代码将在以下行中断:
<input type="submit" name="ctl00$contentBody$buttonSearch" value="Search" onclick="return CheckSearch(document.getElementById('textBoxSearch'),document.getElementById('comboCodes'), 3);" id="contentBody_buttonSearch" />
错误消息为:Microsoft JScript运行时错误:预期为对象
请帮忙
我正在使用ASP.NET4.0
谢谢此错误的原因是您没有传递对象,而是传递了对象的ID。解决办法可能是
OnClientClick="return CheckSearch('<%= textBoxSearch %>','<%= comboCodes %>', 3);
OnClientClick=“return CheckSearch(“”,”,3);
用OnClientClick替换它,然后检查它是否工作。您可能希望将
CheckSearch
函数更改为类似以下内容:
function CheckSearch(name, code, iMinSize) {
var textBox, comboBox; // use these variables
textBox = document.getElementById(name);
comboBox = document.getElementById(code);
if (textBox.value.length < iMinSize && comboBox.value === '') {
alert('You must enter ' + iMinSize + ' letters or more when searching.');
textBox.focus();
return false;
}
else {
return true;
}
}
document.getElementById('textBoxSearch')
这样,您将传递对组合框和文本框的引用,而不仅仅是id字符串
希望是最后一次编辑:
我没有过多地研究它,但是当我运行代码时,我注意到就像你的一样,一些字符没有被转义,所以它呈现如下:
function CheckSearch(name, code, iMinSize) {
var textBox, comboBox; // use these variables
textBox = document.getElementById(name);
comboBox = document.getElementById(code);
if (textBox.value.length < iMinSize && comboBox.value === '') {
alert('You must enter ' + iMinSize + ' letters or more when searching.');
textBox.focus();
return false;
}
else {
return true;
}
}
document.getElementById('textBoxSearch')
(这是可以解决的,但我现在没有时间去做)
因此,函数将接收null
对象,因为它无法通过该参数找到它,所以我最终执行了以下操作:
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="textBoxSearch" runat="server" />
<asp:DropDownList ID="comboCodes" runat="server" >
<asp:ListItem Text=""></asp:ListItem>
<asp:ListItem Value="1" Text="One"></asp:ListItem>
<asp:ListItem Value="2" Text="Two"></asp:ListItem>
<asp:ListItem Value="3" Text="Drei"></asp:ListItem>
<asp:ListItem Value="4" Text="Four"></asp:ListItem>
</asp:DropDownList>
<asp:Button ID="buttonSearch" runat="server" Text="Search" OnClick="buttonSearch_Click"
OnClientClick="return CheckSearch(3);" />
</div>
</form>
<script type="text/javascript">
function CheckSearch(iMinSize) {
var textBox, comboBox;
textBox = document.getElementById('<%= textBoxSearch.ClientID %>');
comboBox = document.getElementById('<%= comboCodes.ClientID %>');
if (textBox.value.length < iMinSize && comboBox.value === '') {
alert('You must enter ' + iMinSize + ' letters or more when searching.');
textBox.focus();
return false;
}
else {
return true;
}
}
</script>
</body>
函数检查搜索(iMinSize){
变量文本框,组合框;
textBox=document.getElementById(“”);
comboBox=document.getElementById(“”);
if(textBox.value.length
注意这并不理想,因为您的函数不够通用,无法处理其他控件,因为我在函数本身中获取控件的引用。尽管如此,我建议您按照Mike Christensen示例(+1)的思路执行操作因为您在调用时会有对控件的引用,并且不会有字符转义的问题,但是我还没有测试他的代码tho
希望这对您有所帮助。问题是您正在发送harcoded文本:
'textBoxSearch'、'comboCodes'
。您应该发送客户端ID:
OnClientClick="javascript:return CheckSearch('<%= textBoxSearch.ClientId %>','<%= comboCodes.ClientId %>', 3);"
OnClientClick=“javascript:返回CheckSearch(“”,,,3);"
在页面底部,我会添加对各种客户端ID的引用。例如:
<script type="text/javascript">
var buttonSearch = document.getElementById('<%= buttonSearch.ClientID %>');
var textBoxSearch = document.getElementById('<%= textBoxSearch.ClientID %>');
var comboCodes = document.getElementById('<%= comboCodes.ClientID %>');
</script>
现在,您可以通过以下方式访问此文本框的值:
document.getElementById('textBoxSearch').value;
OnClientClick
不需要URL。删除javascript:
。真的,你应该提高你的接受度……这些答案对你有帮助吗?你的想法是对的,但也不管用。你只是传递控件的客户端ID,而不是实际的DOM对象。javascript函数需要这样做例如,为了调用name.value
,我编辑了我看到使用ClientID的那一分钟,这是毫无意义的,并且会产生相同的错误:)我尝试更改示例的方式,但仍然收到错误。代码在以下行中断:错误消息是:Microsoft JScript运行时错误:object expectedI修复了上述错误。我现在收到以下错误:Microsoft JScript运行时错误:“value.length”为null或不是对象。感谢您的回复。我已更改代码,使其与您的第二次编辑类似。但是我认为问题出在我的JS函数中。如果我尝试运行“alert(name.length)”,则“这不会运行,函数会在这里停止,没有任何错误。有什么想法吗?textBoxSearch.ClientClick?什么???@MilkyWayJoe哈哈,对不起,我是说ClientId!我想:)但是,我想这可能会呈现&
,而不是像我在回答中说的那样,我没有测试过这个,但这似乎是最好的方法ClientDMode=“Static”是我的解决方案,谢谢!
<asp:TextBox ID="textBoxSearch" runat="server" ClientIDMode="Static" />
document.getElementById('textBoxSearch').value;