函数不使用.click进行触发

时间:2014-09-07 11:29:01

标签: javascript jquery

我试图使用$(this)在click事件上触发一个函数,可能是为了捕获事件并触发一个函数。我尝试了各种方法,但仍然在firebug中遇到错误

  参数列表

之后

SyntaxError:missing)

如果有人能够根据我的代码解释如何实现这一目标,我将不胜感激。非常感谢

$(function() { 
$("#box_frtv").chosen({

 width: "250px",
 max_selected_options: 1

 });
 $("#box_frtv").bind("chosen:maxselected",

            // Only want this function to fire onclick event. something like $(this).click( 
            function() {

            $("#boxerror").freeow("ERROR", "sample error message", {
                classes: ["gray", "error"],
                autoHide: true
            });
          });
        }); 

更新的代码仍有错误。

$(function() { 
    $("#box_frtv").chosen({
        width: "250px",
        max_selected_options: 1
    });

    $("#box_frtv").bind("chosen:maxselected",
        // Only want this function to fire onclick event. something like  
        $(this).click( function() {
            $("#boxerror").freeow("ERROR", "sample error message", {
                classes: ["gray", "error"],
            autoHide: true
            });
        });
    }); 
});

HTML

<div class="fieldset">
            <h1><span>Select Your Box(es)</span></h1>
            <p>
              <select data-placeholder="Choose your box(es)..." class="chosen-select" name="box_frtv[]" id="box_frtv" multiple required="required">
                <option value=""></option>
                <?php
                  do {

                  ?>
                <option value="<?php echo $row_rs_fbrtv['boxref']; ?>"><?php echo $row_rs_fbrtv['boxref']; ?></option>
                <?php
                  } while ($row_rs_fbrtv = mysql_fetch_assoc($rs_fbrtv));

                  $rows = mysql_num_rows($rs_fbrtv);

                  if($rows > 0) {

                  mysql_data_seek($rs_fbrtv, 0);

                  $row_rs_fbrtv = mysql_fetch_assoc($rs_fbrtv);

                  }

                  ?>
              </select><span id="boxerror" class="freeow freeow-bottom-right"></span><div id="boxError"></div>
            </p>
          </div>

2 个答案:

答案 0 :(得分:1)

试试这段代码:

$(function() { 
    $("#box_frtv").chosen({
        width: "250px",
        max_selected_options: 1
    });

    $("#box_frtv").bind("chosen:maxselected",function(){
        // Only want this function to fire onclick event. something like  
        $(this).click( function()
        {
            $("#boxerror").freeow("ERROR", "sample error message",
            {
                classes: ["gray", "error"],
                autoHide: true
            });
        });
    });

 });

您已撰写$("#box_frtv").bind("chosen:maxselected",而不是$("#box_frtv").bind("chosen:maxselected",function(){

如果这样可以解决您的问题!

答案 1 :(得分:0)

你必须将回调代码放在这样的函数中:

$("#box_frtv").bind("chosen:maxselected", function() {
    $(this).click();
});

为了触发click事件,click()函数不能有任何参数。因此,您可以将回调函数绑定到click事件:

$("#box_frtv").bind("click", function() {
    $("#boxerror").freeow("ERROR", "sample error message", {
        classes: ["gray", "error"],
        autoHide: true
    });
});