使用OnClick进行JavaScript函数调用

时间:2015-05-07 13:39:21

标签: javascript html onclick

我有一个简单的函数,它在硬编码时有效,但是当我尝试将第二个参数传递给它时,它不起作用。我使用id => thumbnail调用函数并使用<script> function clearFileInputField(tagId) { document.getElementById(tagId).innerHTML = document.getElementById(tagId).innerHTML; $('.thumbnail').val(""); } </script> <div id="thumbnail_div" class="row"> <?php echo $form->labelex($model,'thumbnail'); ?> <?php echo $form->textfield($model,'thumbnail', array(placeholder => "No file chosen", readonly => true, 'class' => 'thumbnail')); ?><br> <?php echo $form->filefield($model,'thumbnail'); ?> <?php echo $form->error($model,'thumbnail'); ?> <input type="checkbox" onclick = "clearFileInputField('thumbnail_div')" href="javascript:noAction();"> Remove Thumbnail </div> 来获取值。有什么建议吗?

硬编码示例(作品)

 <script>
   function clearFileInputField(tagId, div) {
      document.getElementById(tagId).innerHTML = document.getElementById(tagId).innerHTML;
      $('.div').val("");
    }
    </script>

    <div id="thumbnail_div" class="row">
        <?php echo $form->labelex($model,'thumbnail'); ?>
        <?php echo $form->textfield($model,'thumbnail', array(placeholder => "No file chosen", readonly => true, 'id' => 'thumbnail')); ?><br>
        <?php echo $form->filefield($model,'thumbnail'); ?>
        <?php echo $form->error($model,'thumbnail'); ?>

        <input type="checkbox" onclick = "clearFileInputField('thumbnail_div', 'thumbnail')" href="javascript:noAction();"> Remove Thumbnail
    </div>

传递的参数(不工作)

  CustomResource customResource;

@Override
public Resources getResources() {
    if (customResource == null) {
        customResource = new CustomResource(super.getAssets(), super.getResources().getDisplayMetrics(), super
                .getResources().getConfiguration());
    }

    return customResource;
}

public class CustomResource extends Resources {

    public CustomResource(AssetManager assets, DisplayMetrics metrics, Configuration config) {
        super(assets, metrics, config);

    }

    @Override
    public int getColor(int id) throws NotFoundException {
        Log.d("TAG", "id: " + id + "   getResourceName: " + getResourceName(id));
        Log.i("TAG", "Came Color: "+super.getColor(id));
        return getColor ( getResourceName(id) , id);
    }

    public int getColor(String aString, int oldId) {
        String packageName = getPackageName();
        int resId = getIdentifier(getThemeColorName(aString),null,null);
        Log.v("TAG", "resId: "+resId);
        if (resId != 0) {
            Log.i("TAG", "We SET Color: "+super.getColor(resId));
            return super.getColor(resId);
        } else {
            return super.getColor(oldId);
        }
    }

    private String getThemeColorName(String aString){
        int pos = aString.lastIndexOf('/');
        if (pos != -1){
            aString = aString.substring(0, pos+1) + "b" + aString.substring(pos+1, aString.length());
        }

        Log.d("TAG", "aString NOW: " + aString);

        return aString;
    }

}

5 个答案:

答案 0 :(得分:4)

你快到了。在您的代码中,参数div被转换为字符串。而不是那样,尝试下面给出的代码,

<script>
       function clearFileInputField(tagId, div) {
                document.getElementById(tagId).innerHTML = 
                              document.getElementById(tagId).innerHTML;
                    $('.'+div).val("");        
                }
        </script>

答案 1 :(得分:3)

$('.div').val("");
  ^^^^^^

这是一个字符串,而不是一个变量。您正在尝试查找包含class="div"的元素。

您需要将变量与包含点的字符串连接起来。:

$('.' + div).val("");

答案 2 :(得分:2)

$('.div').val(""); 

那部分很接近但不会像你想象的那样起作用。相反,你应该有两种方式之一,

$('.'+div).val("");

,或者

$(div).val("");

使用选项1,您在句点中使用字符串并将其与变量 div

的值连接起来

使用选项2,您需要更改传递的参数以包含之前的句点。

答案 3 :(得分:1)

您可以轻松摆脱内联处理程序,只需创建一个简单的事件处理程序。

@PostLoading
jQuery(function(){
  // Bind a handler to any button with the class remove_thumbnail
  $('.remove_thumbnail').change(function(){
    if (this.checked) {     
      $(this)
        // go up to parent row
        .parents('.row')
        // find the thumbnail
        .find('.thumbnail')
        .val("");
    }
  });
});

这里的优点是您可以分离内容和行为,而不会将功能引入全局范围。

答案 4 :(得分:-2)

试试这个(将脚本放在标记下面):

<div id="thumbnail_div" class="row">
    <?php echo $form->labelex($model,'thumbnail'); ?>
    <?php echo $form->textfield($model,'thumbnail', array(placeholder => "No file chosen", readonly => true, 'id' => 'thumbnail')); ?><br>
    <?php echo $form->filefield($model,'thumbnail'); ?>
    <?php echo $form->error($model,'thumbnail'); ?>

    <input type="checkbox" onclick = "clearFileInputField('thumbnail_div', 'thumbnail')" href="javascript:noAction();"> Remove Thumbnail
</div>

 <script>
        function clearFileInputField(tagId, div) {
            document.getElementById(tagId).innerHTML = document.getElementById(tagId).innerHTML;
            $('.'+div).val("");

        }
</script>