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;
}
}
現在、マークアップで文字列のみを渡していますが、関数はオブジェクトを期待しています。
編集: または、関数をそのままにして、マークアップを次のように変更することもできます。
編集2:文字列を変更しましたdocument.getElementById
<asp:Button ID="buttonSearch" runat="server" Text="Search" OnClick="buttonSearch_Click"
OnClientClick="return CheckSearch(document.getElementById('<%= textBoxSearch.ClientID %>'),document.getElementById('<%= comboCodes.ClientID %>'), 3);" />
このようにして、id 文字列だけでなく、コンボとテキスト ボックスへの参照を渡します。
うまくいけば、最後の編集:
あまり詳しく調べていませんでしたが、そのコードを実行したときに、あなたと同じように、一部の文字がエスケープされていないことに気付きました。そのため、次のようにレンダリングしていました。
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>
関数自体でコントロールの参照を取得しているため、関数は他のコントロールを処理するのに十分な汎用性がないため、これは理想的ではないことに注意してください。そうは言っても、呼び出し時にコントロールへの参照があり、文字エスケープに問題がないため、Mike Christensen の例 (+1) に沿って何かを行うことをお勧めしますが、彼のコードはテストしていません。とも。
お役に立てれば