0

予期しないエラーが発生し、デバッグに少し煩わしい時間を過ごしています。私が得るエラーはこれです:

Uncaught SyntaxError: Unexpected token }

これはHTMLです:

<html>
<head>
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</head>
<body>

<form class="search" action="search" method="get">
<input type="text" id="search-string" />
<button type="button" onclick="return clearInputValue( $("#search-string") )">
<span class="clear-icon"></span>
</button>
</form>

 <script>
 var  clearInputValue = function(a) {

 // Ultimately I will check if the object passed to this function has a value greater than 1, and if so, I will clear the input

   return false;

 };

 </script>


</body>
</html>
4

2 に答える 2

2

別の引用符のセットを使用する必要があります。

<button type="button" onclick="return clearInputValue( $('#search-string') )">

一重引用符に注意してください。

また、補足として、インライン JavaScript はお勧めしません。プレゼンテーション (css) と同じように、コンテンツ (html) を動作 (javascript) から分離しておく方がはるかに優れています。

于 2012-05-29T00:24:31.013 に答える
2

1 つは、文字列を早期に終了したことです。次のように変更します。

onclick="return clearInputValue($('#search-string'))"

次に、代わりに jQuery を使用してイベントをアタッチする必要があります。

$(function(){

    function clearInputValue(a) {
        return false;
    };


    $('button').on('click',function(){
        clearInputValue($('#search-string'));
    });

});
于 2012-05-29T00:26:09.067 に答える