HTML输入框未触发事件

时间:2021-11-30 18:33:07

I have the following bit of javascript

我有以下一点javascript

function select(id, evt) { 
        var e = (typeof evt != 'undefined') ? evt : event;
        e.cancelBubble = true;
        alert('err');
        var $toggler = $("#building"+id);     

        if(!$toggler.is(':checked')){
            $toggler.attr('checked', true);
            $("#row"+id).removeClass("selectRow");
            $("#row"+id).addClass("selectRow");            
        } 
    }

I have the following <HTML> block

我有以下块

<td>
   <select onchange="select(2, event);" name="prefferedAgent396" class="input-small">
       <option value="0">Choose...</option>
       <option value="1" selected=""> Senthil </option>                                    
       <option value="3"> Sunmeet </option>                                    
       <option value="4"> Doesnot Speak English </option>                                    
       <option value="5"> Marcus Fava </option>                                    
   </select> 
</td>
<td>
   <input type="text" onchange="select(2, event);" class="input-mini" id="initial396" name="initial396" value="">
</td>

Now the problem is the onchange event triggers the select(2, event) for the select control but not the input control. When i tried an alert box where I call select() it worked but its not simply calling the select(). I have tried onKeyUp but it gave a strange behavior of replacing the text as I type. What am i doing wrong here?

现在问题是onchange事件触发select控件的select(2,event)而不是输入控件。当我尝试一个警报框,我调用select()它工作,但它不是简单地调用select()。我尝试过onKeyUp,但它给出了一个奇怪的行为,即在我键入时替换文本。我在这做错了什么?

1 个解决方案

#1


1  

That's very strange. If you rename the function from select to, say select_, it will work. But since you're already using jQuery, you can use the following instead of using the inline change handlers:

这很奇怪。如果您将该功能从select重命名为,例如select_,它将起作用。但由于您已经在使用jQuery,因此可以使用以下代码而不是使用内联更改处理程序:

$("#prefferedAgent396,#initial396").change(function (e) {
   select(2, e);
});

Here's a DEMO.

这是一个DEMO。

#1


1  

That's very strange. If you rename the function from select to, say select_, it will work. But since you're already using jQuery, you can use the following instead of using the inline change handlers:

这很奇怪。如果您将该功能从select重命名为,例如select_,它将起作用。但由于您已经在使用jQuery,因此可以使用以下代码而不是使用内联更改处理程序:

$("#prefferedAgent396,#initial396").change(function (e) {
   select(2, e);
});

Here's a DEMO.

这是一个DEMO。