简体   繁体   English

使用OnClick进行JavaScript函数调用

[英]JavaScript Function Call With OnClick

I have a simple function that works when it is hard coded, but when I try to pass a second parameter into it, it doesn't work. 我有一个简单的函数,当对其进行硬编码时可以使用,但是当我尝试将第二个参数传递给它时,它不起作用。 I am calling the function with the onclick and using the id => thumbnail to get the value. 我用onclick调用该函数,并使用id => thumbnail获取值。 Any suggestions? 有什么建议么?

Hard Coded Example (Works) 硬编码示例(作品)

<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>

Parameters Passed (Not Working) 传递的参数(不起作用)

 <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>

You are almost there. 你快到了。 In your code, the paramenter div is converted to string. 在您的代码中,paramenter div被转换为字符串。 Instead of that, try the code given below, 而是尝试下面给出的代码,

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

That's a string, not a variable. 那是一个字符串,而不是一个变量。 You're trying to find elements that have class="div" . 您正在尝试查找具有class="div"元素。

You need to concatenate the variable with a string containing the dot.: 您需要将变量与包含点的字符串连接起来:

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

That part is close but not going to work as you might have intended. 该部分很接近,但无法按预期工作。 You instead should have it one of two ways, 相反,您应该使用以下两种方法之一,

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

or, 要么,

$(div).val("");

With option 1, you are using a string for the period and concatenating it with the value of the variable div 使用选项1时,您在句点中使用字符串并将其与变量div的值连接

With option 2, you will need to change the passed parameter to include a period before it. 对于选项2,您将需要更改传递的参数以在其之前包含句点。

You could easily get rid of your inline handler and just create a simple event handler. 您可以轻松摆脱内联处理程序,而只需创建一个简单的事件处理程序即可。

 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(""); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="thumbnail_div" class="row"> <input type="text" class="thumbnail" value="foo"> <input type="checkbox" class="remove_thumbnail"> Remove Thumbnail </div> 

The advantages here are that you separate content and behavior and do not introduce functions into the global scope. 这样做的好处是您将内容和行为分开,并且不将函数引入全局范围。

try this (with script placed underneath the markup): 试试看(将脚本放置在标记下方):

<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM