0

我的 <head> 中有这个脚本:

<script>
var rad = document.getElementsByName('search_type');
alert(rad[1]);
var prev = null;
for (var i = 0; i < rad.length; i++) {
    rad[i].onclick = function() {
        (prev)? console.log(prev.value):null;
        if(this !== prev) {
            prev = this;
        }
        console.log(this.value)
        alert(this.value);
    };
}
</script>

这是我的表格:

<form name="search_form" action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 
    <table border="0"> 
        <tr>
            <td class="input">
                <input type="radio" name="search_type" value="search_code" checked>1st type<br>
                <input type="radio" name="search_type" value="search_title">2nd type<br>
                <input type="radio" name="search_type" value="search_filter">3rd type<br>
            </td>
        <tr/>
    </table> 
</form>

但所有警报都不起作用。我在控制台中没有错误。请帮忙。

4

4 回答 4

4

在解析单选按钮的 HTML 代码之前,您正在运行脚本。

将脚本放在表单下方,或在加载事件中运行代码:

window.onload = function() {

  var rad = document.getElementsByName('search_type');
  alert(rad[1]);
  var prev = null;
  for (var i = 0; i < rad.length; i++) {
    rad[i].onclick = function() {
      (prev)? console.log(prev.value):null;
      if(this !== prev) {
        prev = this;
      }
      console.log(this.value)
      alert(this.value);
    };
  }

};
于 2012-11-27T08:05:02.587 回答
2

你可以用一些 jQuery 来简化你的代码:

$(document).ready(function() {
    var rad = $('[name="search_type"]');
    console.log(rad);
    var prev = null;
    rad.click(function() {
        console.log(prev, $(this).is(':checked'));
        if (this !== prev) {
            prev = this;
        }
        console.log(prev, $(this).is(':checked'));
    });
});

http://jsfiddle.net/f2aDK/

于 2012-11-27T08:12:18.277 回答
2

在表单之后移动它的脚本。在浏览器解析 DOM 元素之前,javascript 无法读取 DOM。

<form name="search_form" action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> 
    <table border="0"> 
        <tr>
            <td class="input">
                <input type="radio" name="search_type" value="search_code" checked>1st type<br>
                <input type="radio" name="search_type" value="search_title">2nd type<br>
                <input type="radio" name="search_type" value="search_filter">3rd type<br>
            </td>
        <tr/>
    </table> 
</form>

<script>
var rad = document.getElementsByName('search_type');
alert(rad[1]);
var prev = null;
for (var i = 0; i < rad.length; i++) {
    rad[i].onclick = function() {
        (prev)? console.log(prev.value):null;
        if(this !== prev) {
            prev = this;
        }
        console.log(this.value)
        alert(this.value);
    };
}
</script>
于 2012-11-27T08:25:03.940 回答
0

首先添加jquery库

 $(document).ready(function () {
             $('[name="search_type"]').live('click', function (event) {
                     var rad = document.getElementsByName('search_type');
                        alert(rad[1]);
                       var prev = null;
                       for (var i = 0; i < rad.length; i++) {
                           rad[i].onclick = function() { 
                               (prev)? console.log(prev.value):null;
                               if(this !== prev) {
                                prev = this;
                                }
                              console.log(this.value)
                              alert(this.value);
                            };
                        }
    });
    });
于 2012-11-27T08:15:31.100 回答