动态创建的选择列表可以对muavascript函数进行多次调用

时间:2016-08-11 11:12:58

标签: javascript php html

我已经动态创建了多个选择列表。点击频道名称,它应该得到它的类型。问题是一旦点击选择列表,它会重复调用java脚本函数,导致ajax多次加载 HTML CODE:

<td>
                                <SELECT name="channel_name[]" onclick ="get_type(this)"; required class='channelname'>
                                        <option value="">Select...</option>
                                      <?php foreach($channel_list as $row) {
                                                $channelid = $row['channelid'];
                                                $channelname = $row['channelname'];

                                                 if($U_channelid==$channelid)
                                                {
                                                        $s = "selected = selected";
                                                }
                                                else
                                                {
                                                        $s = "";
                                                }
                                                echo "<option value='$channelid' $s>".$channelname."</option>";
                                        ?>
<!--                                    <OPTION value='<?php echo $channelid ?>' $s ><?php echo $channelname?></OPTION> -->

                                <?php } ?>
                                </SELECT>
                        </td>

Javascipt代码:

function get_type()
{
        $(".channelname").live("change", function() {

                var channel_id = $(this).find("option:selected").attr("value");
                var _this = $(this); //Save current object
                alert(channel_id);
                $.ajax({
                        type: "POST",
                        url: '<?php echo base_url(); ?>index.php/partner/get_channel_type',
                        data: 'channelid='+channel_id,
                        async:   false
                         }).done(function( data1 ) {

                        if(data1){
                                _this.closest("tr").find('input[name="type[]"]').val(data1);

                        }else{
                                alert("Channel type is not defined");
                                 _this.closest("tr").find('input[name="type[]"]').val("");

                        }


                });
    });
}

1 个答案:

答案 0 :(得分:0)

从select标签中移除onclick =“get_type(this)”//因为你已经使用$(“。channelname”)。live(“change”,function(){in javascript

把这个

<SELECT name="channel_name[]" required class='channelname'>

和javascript

$(".channelname").change(function() {

                var channel_id = $('.channelname').find("option:selected").attr("value");
                alert(channel_id);
                $.ajax({
                        type: "POST",
                        url: '<?php echo base_url(); ?>index.php/partner/get_channel_type',
                        data: 'channelid='+channel_id,
                        async:   false
                         }).done(function( data1 ) {

                        if(data1){
                                _this.closest("tr").find('input[name="type[]"]').val(data1);

                        }else{
                                alert("Channel type is not defined");
                                 _this.closest("tr").find('input[name="type[]"]').val("");

                        }


                });
    });